Plotly 中的 Python_DF 排序和自定义数据

我在下面的代码中悬停时遇到错误的反射数据问题。请参阅每个块的注释代码。


我在下面的代码中悬停时遇到错误的反射数据问题。请参阅每个块的注释代码。


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

所以最后我猜想这个问题是由订购引起的,也许我需要在自定义数据行中重新制作 smth,但无法理解如何解决它。将感谢您帮助修复它。

http://img1.mukewang.com/63a161cb00017f6602450142.jpg

慕标5832272
浏览 137回答 1
1回答

元芳怎么了

在这种情况下,我很难使用自定义悬停模板(你最终可以看到这个文档)但我认为我可以在不添加额外跟踪的情况下实现你正在寻找的输出。fig=px.scatter_geo(df_ordered,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;lon='longitude',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;lat='latitude',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;color='bins',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;color_discrete_sequence=px.colors.qualitative.Set1,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;hover_name="names",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;size='data',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;opacity=0.7,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;text='names',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;projection="equirectangular",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;size_max=35,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# by default every column go to hover&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# you can eventually use formatting here&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;hover_data={"longitude": False,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"latitude": False,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"names": False,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"data": ":.2f"},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# if you don't want to change column names&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# you can just change them here&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;labels={"bins": "Bin",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"data": "Data"}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;)fig.update_traces(mode="markers+text",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; textposition="middle left",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; textfont=dict(size=12,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; color="black")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; showlegend=False,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;)# Here I just change the `=` for `: ` in every tracefor data in fig.data:&nbsp; &nbsp; data.hovertemplate = data.hovertemplate.replace("=", ": ")fig.show()更新我刚刚意识到有一个错误与labels一起使用hover_data,特别是如果您labels出于某种原因使用格式“数据”:“:.2f”未保留。可能的解决方法如下fig = px.scatter_geo(df_ordered,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;lon='longitude',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;lat='latitude',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;color='bins',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;color_discrete_sequence=px.colors.qualitative.Set1,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;hover_name="names",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;size='data',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;opacity=0.7,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;text='names',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;projection="equirectangular",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;size_max=35,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# by default every column go to hover&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# you can eventually use formatting here&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;hover_data={"longitude": False,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"latitude": False,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"names": False,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"data": ":.2f"}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )fig.update_traces(mode="markers+text",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; textposition="middle left",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; textfont=dict(size=12,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; color="black"),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; showlegend=False,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;)# it's pretty verbose but now the output should be# exactly as you expectfor data in fig.data:&nbsp; &nbsp; template = data.hovertemplate&nbsp; &nbsp; template = template.replace("<b>", "<b>Name: ")\&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.replace("bins=", "Bin: ")\&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.replace("data=", "Data: ")&nbsp; &nbsp; data.hovertemplate = templatefig.show()
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python