Search code examples
pythonhoverplotlyscatter

Python plotly scatter_geo modify hover data


I want to modify hover data and leave ther for e.g. only bins data. I made following code, but hover_data parameter didn't work. What is the way to modify haver data?

import plotly.express as px
import plotly.graph_objs as go
import pandas as pd

rows=[['501-600','15','122.58333','45.36667'],
      ['till 500','4','12.5','27.5'],
      ['more 1001','41','-115.53333','38.08'],
      ]

colmns=['bins','data','longitude','latitude']
df=pd.DataFrame(data=rows, columns=colmns)
df = df.astype({"data": int})

fig=px.scatter_geo(df,lon='longitude', lat='latitude',
                      color='bins',
                      opacity=0.5,
                      size='data',
                      projection="natural earth", hover_data=(['bins']))

fig.add_trace(go.Scattergeo(lon=df["longitude"],
              lat=df["latitude"],
              text=df["data"],
              textposition="middle center",
              mode='text',
              showlegend=False))
fig.show()

Solution

  • you can mention as below for hover_data argument of scatter_geo.

    import plotly.express as px
    import plotly.graph_objs as go
    import pandas as pd
    
    rows=[['501-600','15','122.58333','45.36667'],
          ['till 500','4','12.5','27.5'],
          ['more 1001','41','-115.53333','38.08'],
          ]
    
    colmns=['bins','data','longitude','latitude']
    df=pd.DataFrame(data=rows, columns=colmns)
    df = df.astype({"data": int})
    
    fig=px.scatter_geo(df,lon='longitude', lat='latitude',
                          color='bins',
                          opacity=0.5,
                          size='data',
                          projection="natural earth", hover_data={'longitude':False,'latitude':False,'data':False})
    
    fig.add_trace(go.Scattergeo(lon=df["longitude"],
                  lat=df["latitude"],
                  text=df["data"],
                  textposition="middle center",
                  mode='text',
                  showlegend=False))
    
    
    fig.show()
    

    Setting the columns name as False in hover_data will remove that column name from hover_data. Hope this answers your question.