我在下面的代码中遇到了悬停中反射数据错误的问题。请看带注释的代码。
在下面的代码中,我有一个关于悬停的反射数据错误的问题。请看代码,每块都有注释。
import plotly.express as px
import pandas as pd
import plotly.graph_objects as go
rows=[['501-600','15','122.58333','45.36667','Name1'],
['till 500','4','12.5','27.5','Name2'],
['more 601','41','-115.53333','38.08','Name3'],
['till 500', '26', '65.5', '29.5','Name4'],
['501-600','35','12.58333','55.36667','Name5'],
['more 601','9','55.53333','-38.08','Name6'],
]
colmns=['bins','data','longitude','latitude','names']
#Df creation
df=pd.DataFrame(data=rows, columns=colmns)
#Ordering for labels in legend
order = ['till 500', '501-600', 'more 601']
df = df.set_index('bins')
df_ordered = df.T[order].T.reset_index()
df_ordered = df_ordered.astype({"data": int})
#Plotting viz
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='data',
projection="equirectangular",size_max=35,
)
#Adding custom data for hovers
fig.update_traces(customdata=df_ordered)
fig.update_traces(hovertemplate="<b>Name: %{customdata[4]} </b><br><br>Bin: %{customdata[0]}<br>"
"Data: %{customdata[1]:.2f}<extra></extra>")
#Adding marker labels
fig.add_trace(go.Scattergeo(lon=df_ordered["longitude"],
lat=df_ordered["latitude"],
text=df_ordered["names"],
textposition="middle left",
mode='text',
textfont=dict(size=12,color="black"),
showlegend=False,
texttemplate=" %{text}",
hoverinfo='skip',
))
fig.show()
所以在最后我猜测这个问题是由排序引起的,也许我需要在customdata行重做一些东西,但不明白如何解决它。将感谢帮助解决它。
在这种情况下,我在玩自定义悬停模板的时候相当困难(你最终可以看到这一点 文档但我认为我可以在不增加额外的跟踪的情况下实现你所需要的输出。
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()
更新 我刚刚意识到,有一个bug,就是 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()