Python_df Ordering And Customdata In Plotly
I have issue with wrong reflection data in hovers in the code below. See code with comments for each block. I have issue with wrong reflection data in hovers in the code below. See
Solution 1:
I had quite hard time playing with the custom hovertemplate in this case (you can eventually see this doc) But I think that I could achieve the output your looking for without add an extra trace.
fig=px.scatter_geo(df_ordered,
lon='longitude',
lat='latitude',
color='bins',
color_discrete_sequence=px.colors.qualitative.Set1,
hover_name="names",
size='data',
opacity=0.7,
text='names',
projection="equirectangular",
size_max=35,
# by default every column go to hover# you can eventually use formatting here
hover_data={"longitude": False,
"latitude": False,
"names": False,
"data": ":.2f"},
# if you don't want to change column names# you can just change them here
labels={"bins": "Bin",
"data": "Data"}
)
fig.update_traces(mode="markers+text",
textposition="middle left",
textfont=dict(size=12,
color="black")
showlegend=False,
)
# Here I just change the `=` for `: ` in every tracefor data in fig.data:
data.hovertemplate = data.hovertemplate.replace("=", ": ")
fig.show()
Update I just realized that there is a bug with labels
used together with hover_data
in particular if you use labels
for some reasons the formatting "data": ":.2f" is not preserved. A possible workaround is the following
fig = px.scatter_geo(df_ordered,
lon='longitude',
lat='latitude',
color='bins',
color_discrete_sequence=px.colors.qualitative.Set1,
hover_name="names",
size='data',
opacity=0.7,
text='names',
projection="equirectangular",
size_max=35,
# by default every column go to hover# you can eventually use formatting here
hover_data={"longitude": False,
"latitude": False,
"names": False,
"data": ":.2f"}
)
fig.update_traces(mode="markers+text",
textposition="middle left",
textfont=dict(size=12,
color="black"),
showlegend=False,
)
# it's pretty verbose but now the output should be# exactly as you expectfor data in fig.data:
template = data.hovertemplate
template = template.replace("<b>", "<b>Name: ")\
.replace("bins=", "Bin: ")\
.replace("data=", "Data: ")
data.hovertemplate = template
fig.show()
Post a Comment for "Python_df Ordering And Customdata In Plotly"