0

In the code below I tried to use customdata to make hovertemplate, but in this case on visualization it shows only data from the first row everywhere. I believe there should be function, but don't know how to implement it.

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

new_customdata = df.loc[:,('bins', 'data')]
fig.update_traces(go.Scattergeo(
customdata=new_customdata,
hovertemplate="<b>%{customdata[0]} </b><br><br>" + \
              "blablabla: %{customdata[1]: .3f}<extra></extra>"))

fig.show()

Data in hover must be: till 500 and 4

2
  • What do you mean shows data only from the first row? What do you expect to see, and what are you seeing instead? I'm running your code, and it seems to be working fine. Commented May 31, 2020 at 15:53
  • I updated my answer with picture. On red marker data in hover must be - till 500 and 4. So if you look at df, data in hover reflex everywhere from top row of df. Commented May 31, 2020 at 17:03

1 Answer 1

1

I believe this does what you're looking for:

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 = go.Figure(data=go.Scattergeo(
    lon = df['longitude'],
    lat = df['latitude'],
    mode = 'markers', 
    marker_color = df.index, 
    marker_size=df['data'], 
    customdata = df, 

    hovertemplate="<b>%{customdata[0]} </b><br><br>blablabla: %{customdata[1]: .3f}<extra></extra>"

))

fig.show()

The result is (there's different hover text for each item):

enter image description here

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

1 Comment

"marker_color = df.index" does the trick. I added it to the solution,

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.