python - Plotly Geoscatter 与聚合 : show aggregation in hover Info

标签 python plotly

我一直在尝试使用 Plotly 创建地理分散图,其中标记大小应指示一个城市 (zip_city) 中的客户数量(行项目)。我的代码基于 Plotly 文档中的两个模板:United States Bubble Map和聚合部分Mapping with Aggregates .

我成功地编写了一个可以实现我想要的功能的代码,除了一个缺点:当我将鼠标悬停在气泡上时,我希望看到城市名称加上客户数量(聚合的结果),比如Aguadilla: 2。你能帮我解决这个问题吗?

这是我的代码(作为plotly的初学者,我也愿意改进代码):

import plotly.offline as pyo
import pandas as pd

df = pd.DataFrame.from_dict({'Customer': [111, 222, 555, 666],
        'zip_city': ['Aguadilla', 'Aguadilla', 'Arecibo', 'Wrangell'],
        'zip_latitude':[18.498987, 18.498987, 18.449732,56.409507],
        'zip_longitude':[-67.13699,-67.13699,-66.69879,-132.33822]})

data = [dict(
        type = 'scattergeo',
        locationmode = 'USA-states',
        lon = df['zip_longitude'],
        lat = df['zip_latitude'],
        text = df['Customer'],
        marker = dict(
            size = df['Customer'],
            line = dict(width=0.5, color='rgb(40,40,40)'),
            sizemode = 'area'
            ),
        transforms = [dict(
                            type = 'aggregate',
                            groups = df['zip_city'],
                            aggregations = [dict(target = df['Customer'], func = 'count', enabled = True)]
                            )]
        )]


layout = dict(title = 'Customers per US City')

fig = dict( data=data, layout=layout )
pyo.plot( fig, validate=False)

更新:

我可以直接在 data 参数中访问 transforms 参数的结果来显示每个城市的客户数量吗?

最佳答案

您可以创建一个列表,其中包含您想要的内容,然后在data中设置text=list。另外,不要忘记指定 hoverinfo='text'

我更新了你的代码,所以试试这个:

import pandas as pd
import plotly.offline as pyo

df = pd.DataFrame.from_dict({'Customer': [111, 222, 555, 666],
        'zip_city': ['Aguadilla', 'Aguadilla', 'Arecibo', 'Wrangell'],
        'zip_latitude':[18.498987, 18.498987, 18.449732,56.409507],
        'zip_longitude':[-67.13699,-67.13699,-66.69879,-132.33822]})

customer = df['Customer'].tolist()
zipcity = df['zip_city'].tolist()
list = []
for i in range(len(customer)):
    k = str(zipcity[i]) + ':' + str(customer[i])
    list.append(k)

data = [dict(
        type = 'scattergeo',
        locationmode = 'USA-states',
        lon = df['zip_longitude'],
        lat = df['zip_latitude'],
        text = list,
        hoverinfo = 'text',
        marker = dict(
            size = df['Customer'],
            line = dict(width=0.5, color='rgb(40,40,40)'),
            sizemode = 'area'
            ),
        transforms = [dict(
                            type = 'aggregate',
                            groups = df['zip_city'],
                            aggregations = [dict(target = df['Customer'], func = 'count', enabled = True)]
                            )]
        )]

layout = dict(title = 'Customers per US City')

fig = dict(data=data, layout=layout)
pyo.plot(fig, validate=False)  

关于python - Plotly Geoscatter 与聚合 : show aggregation in hover Info,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51662614/

相关文章:

python - Tensorflow:如何按名称获取张量?

python - 在 Dash 中执行回调时,如何为 children 属性中的某些单词设置样式?

python - 如何打开扩展名为 ".plotly"的文件

python - 语法错误 : encoding declaration in Unicode string

python - 极坐标图旁边的垂直 Axis

python - 将多个wx.EVT_MENU绑定(bind)到同一个方法?

python - 从一个列表中查找以另一个列表中的字符串开头的字符串

plotly - 如何更改数字在plot.ly 图表上的显示方式?

reactjs - 用 typescript 对 js 作出 react

javascript - Pyodide Plotly 内存不足,内存访问越界