简体   繁体   English

plotly 库中自定义数据 hover 的 Python_Function

[英]Python_Function for customdata hover in plotly lib

In the code below I tried to use customdata to make hovertemplate, but in this case on visualization it shows only data from the first row everywhere.在下面的代码中,我尝试使用 customdata 来制作悬停模板,但在这种情况下,在可视化上它只显示第一行的数据。 I believe there should be function, but don't know how to implement it.相信应该有function,但是不知道怎么实现。

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

new_customdata = df.loc[:,('bins', 'data')]
fig.update_traces(go.Scattergeo(
customdata=new_customdata,
hovertemplate="<b>%{customdata[0]} </b><br><br>" + \
              "blablabla: %{customdata[1]: .3f}<extra></extra>"))

fig.show()

悬停的数据必须是:直到 500 和 4

I believe this does what you're looking for:我相信这可以满足您的需求:

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 = go.Figure(data=go.Scattergeo(
    lon = df['longitude'],
    lat = df['latitude'],
    mode = 'markers', 
    marker_color = df.index, 
    marker_size=df['data'], 
    customdata = df, 

    hovertemplate="<b>%{customdata[0]} </b><br><br>blablabla: %{customdata[1]: .3f}<extra></extra>"

))

fig.show()

The result is (there's different hover text for each item):结果是(每个项目都有不同的 hover 文本):

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM