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”未保留??赡艿慕鉀Q方法如下
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()
添加回答
舉報