Python plotly scatter_geo 修改悬停数据 [英] Python plotly scatter_geo modify hover data

查看:46
本文介绍了Python plotly scatter_geo 修改悬停数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想修改悬停数据并将其留给例如只有 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.

这篇关于Python plotly scatter_geo 修改悬停数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆