Python plotly scatter_geo 修改悬停数据

2022-01-22 00:00:00 python plotly scatter hover

问题描述

我想修改悬停数据并将其留给例如只有 bins 数据.我做了以下代码,但 hover_data 参数不起作用.修改har数据的方法是什么?

I want to modify hover data and leave ther for e.g. only bins data. I made following code, but hover_data parameter didn't work. What is the way to modify haver data?

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


解决方案

scatter_geo 的 hover_data 参数可以参考如下.

you can mention as below for hover_data argument of scatter_geo.

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={'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 中删除该列名.希望这能回答您的问题.

Setting the columns name as False in hover_data will remove that column name from hover_data. Hope this answers your question.

相关文章