2

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()
Dmitry
  • 361
  • 6
  • 21

3 Answers3

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.

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.

heisenBug
  • 865
  • 9
  • 19
1

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.

jayveesea
  • 2,886
  • 13
  • 25
  • 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? – Dmitry May 21 '20 at 10:28
  • 1
    you are correct, it only appeared to work (at least from me) because the "bins" data was showing up in the `` 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. – jayveesea May 21 '20 at 12:16
  • also, you might find using `go.Scattergeo` inplace of `px.scatter_geo` easier to work with. – jayveesea May 21 '20 at 12:24
  • 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 – Dmitry May 21 '20 at 12:36
  • 1
    added some more info above, but in short, yes you can add multiple fields to `customdata`. I'm still investigating the color part. – jayveesea May 21 '20 at 12:58
  • seems somehow it should be possible to add through `customdata` several data in a way of - `customdata[0]` – Dmitry May 21 '20 at 13:20