3

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

3 Answers 3

5

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.

Sign up to request clarification or add additional context in comments.

5 Comments

now in it's output: RuntimeError: dictionary changed size during iteration
I ran your whole program with changes in hover_data. It gave correct output. Did u change the columns in data frame?
Hi, I updated my comment with entire program that gave expected output for me.
It gave me still the same output. Maybe coz of version of libs?
seems like this should work, but I get the same error.
2

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.

6 Comments

Thx, but if I do like you advised then in viz I just see word bins without any data itself, I also tried to apply info from your link like that: fig.update_traces(hovertemplate ='%{bins}') But in this case I got: %{bins} What do you think?
you are correct, it only appeared to work (at least from me) because the "bins" data was showing up in the <extra> hover field. Adding customdata appears to help (see update above), BUT the color option will break it. I'll continue to look at this and let you know if I come up with anything better.
also, you might find using go.Scattergeo inplace of px.scatter_geo easier to work with.
Seems that "bins" data showing within customdata. Interesting by what logic? And what if I wat to add several data fields to show there? Yet again, I need to save legeng and colors which are there now
added some more info above, but in short, yes you can add multiple fields to customdata. I'm still investigating the color part.
|
2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.