1 回答
TA贡献1798条经验 获得超7个赞
在这种情况下,我很难使用自定义悬停模板(你最终可以看到这个文档)但我认为我可以在不添加额外跟踪的情况下实现你正在寻找的输出。
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 trace
for data in fig.data:
data.hovertemplate = data.hovertemplate.replace("=", ": ")
fig.show()
更新我刚刚意识到有一个错误与labels一起使用hover_data,特别是如果您labels出于某种原因使用格式“数据”:“:.2f”未保留。可能的解决方法如下
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 expect
for data in fig.data:
template = data.hovertemplate
template = template.replace("<b>", "<b>Name: ")\
.replace("bins=", "Bin: ")\
.replace("data=", "Data: ")
data.hovertemplate = template
fig.show()
添加回答
举报