Dmitry
Dmitry

Reputation: 377

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()

Upvotes: 3

Views: 5338

Answers (3)

heisenBug
heisenBug

Reputation: 965

Using the accepted answer, I found the following error (similar to in @Asha Ramprasad's comments):

RuntimeError: dictionary changed size during iteration

as well, for the following:

Python 3.7.6 (default, Jan  8 2020, 13:42:34) 
>>> import plotly
>>> plotly.__version__
'4.4.1'

I removed the error by passing a list of the dataframe columns I wanted, instead of a dictionary:

hover_data = [df["whatever I want displayed"],df["other thing to display"]]

This plotly behavior seems like a bug to me. Note that this doesn't allow removing columns, only adding.

Upvotes: 2

jayveesea
jayveesea

Reputation: 3199

Using Hover Template: A hover template might work well in this situation:

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

fig.update_traces(customdata=df.bins)
fig.update_traces(hovertemplate='Bins: %{customdata}<extra></extra>')

See here and here about using customdata in a hover template.

customdata – Assigns extra data each datum. This may be useful when listening to hover, click and selection events. Note that, “scatter” traces also appends customdata items in the markers DOM elements

Update: using the color option in px.scatter_geo will group the resulting plot's data such that the customdata no longer aligns with the underlining plot data. This is usually the point I abandon plotly express and use plotly go.

Upvotes: 2

Asha Ramprasad
Asha Ramprasad

Reputation: 106

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.

Upvotes: 5

Related Questions