Python plotly scatter_geo 修改悬停数据

我想修改悬停数据并仅保留例如bins数据。我做了以下代码,但hover_data参数不起作用。修改haver数据的方法是什么?


import plotly.express as px

import plotly.graph_objs as go

import pandas as pd


rows=[['501-600','15','122.58333','45.36667'],

      ['till 500','4','12.5','27.5'],

      ['more 1001','41','-115.53333','38.08'],

      ]


colmns=['bins','data','longitude','latitude']

df=pd.DataFrame(data=rows, columns=colmns)

df = df.astype({"data": int})


fig=px.scatter_geo(df,lon='longitude', lat='latitude',

                      color='bins',

                      opacity=0.5,

                      size='data',

                      projection="natural earth", hover_data=(['bins']))


fig.add_trace(go.Scattergeo(lon=df["longitude"],

              lat=df["latitude"],

              text=df["data"],

              textposition="middle center",

              mode='text',

              showlegend=False))

fig.show()


喵喵时光机
浏览 237回答 3
3回答

慕田峪4524236

你可以在下面提到 scatter_geo 的 hover_data 参数。import plotly.express as pximport plotly.graph_objs as goimport pandas as pdrows=[['501-600','15','122.58333','45.36667'],      ['till 500','4','12.5','27.5'],      ['more 1001','41','-115.53333','38.08'],      ]colmns=['bins','data','longitude','latitude']df=pd.DataFrame(data=rows, columns=colmns)df = df.astype({"data": int})fig=px.scatter_geo(df,lon='longitude', lat='latitude',                      color='bins',                      opacity=0.5,                      size='data',                      projection="natural earth", hover_data={'longitude':False,'latitude':False,'data':False})fig.add_trace(go.Scattergeo(lon=df["longitude"],              lat=df["latitude"],              text=df["data"],              textposition="middle center",              mode='text',              showlegend=False))fig.show()在 hover_data 中将列名设置为 False 将从 hover_data 中删除该列名。希望这能回答您的问题。

胡说叔叔

使用接受的答案,我发现了以下错误(类似于@Asha Ramprasad 的评论):RuntimeError: dictionary changed size during iteration同样,对于以下内容:Python 3.7.6 (default, Jan  8 2020, 13:42:34) >>> import plotly>>> plotly.__version__'4.4.1'我通过传递我想要的数据框列列表而不是字典来消除错误:hover_data = [df["whatever I want displayed"],df["other thing to display"]]这种阴谋的行为对我来说似乎是一个错误。请注意,这不允许删除列,只能添加。

阿波罗的战车

使用悬停模板:悬停模板在这种情况下可能效果很好:fig=px.scatter_geo(df,lon='longitude', 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; opacity=0.5,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; size='data',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; projection="natural earth")fig.update_traces(customdata=df.bins)fig.update_traces(hovertemplate='Bins: %{customdata}<extra></extra>')有关在悬停模板中 使用的信息,请参见此处和此处。customdatacustomdata – 为每个数据分配额外的数据。这在监听悬停、点击和选择事件时可能很有用。请注意,“分散”跟踪还在标记 DOM 元素中附加自定义数据项更新:使用中的color选项px.scatter_geo将对结果图的数据进行分组,这样customdata就不再与带下划线的图数据对齐。这通常是我放弃 plotly express 而使用 plotly go 的地方。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python