编程语言
首页 > 编程语言> > python-绘制hoverinfo显示所有数据点而不是当前点

python-绘制hoverinfo显示所有数据点而不是当前点

作者:互联网

我在获取plotly的hoverinfo属性以显示给定点的单个值时遇到麻烦.作为参考,我正在地图上绘制一堆点,并希望能够将鼠标悬停在一个点上并查看其唯一标识符.如果我没有为hoverinfo或text设置任何值,则在悬停时会看到单个点的lat和lon值.但是,当我设置text = nodes.Node和hoverinfo =“ text”时,将鼠标悬停在任意点时会看到所有节点的列表.下面的代码提供了一个最小的示例(在jupyter笔记本中):

import pandas as pd
import plotly.offline as py
from plotly.graph_objs import *
py.init_notebook_mode()

nodes = pd.DataFrame({
    'Node': [103,131,136,143,153],
    'Lat': [39.97703048,39.98315706,40.02686848,40.02110808,40.01174032],
    'Lon': [-83.00179533,-82.97803884,-82.97319305,-83.01509991,-82.97285888]
})
mapbox_access_token = some_mapbox_token
data = Data([
    Scattermapbox(
        lat=nodes.Lat,
        lon=nodes.Lon,
        mode='markers',
        marker=Marker(
            size=2,
            color='red',
            opacity=0.7
        ),
        text=nodes.Node,
        hoverinfo='text'
    )]
)
layout = Layout(
    title='Nodes interacting with busiest TAZ',
    autosize=True,
    hovermode='Closest',
    showlegend=False,
    mapbox=dict(
        accesstoken=mapbox_access_token,
        bearing=0,
        center=dict(
            lat=39.983333,
            lon=-82.983333
        ),
        pitch=0,
        zoom=7.5
    ),
)

我设置的文字不正确吗?还是与hoverinfo或hovermode有关?

解决方法:

对我来说似乎是错误或未指定的行为.您的代码看起来非常好,您只需要传递一个字符串列表而不是整数即可,它应该可以工作.

    text=[str(n) for n in nodes.Node]

enter image description here

标签:scatter-plot,plotly,python
来源: https://codeday.me/bug/20191026/1934416.html