猿问

带有聚合的绘图地理散布:在悬停中显示聚合信息

我一直在尝试用Plotly创建Geoscatter Plot,其中标记大小应指示一个城市(zip_city)中的客户(行项目)数量。我的代码基于Plotly文档中的两个模板:United States Bubble Map和Aggregation part with Aggregates映射。


除了一个缺点,我设法编写了符合我想要的代码:当我将鼠标悬停在气泡上时,我想查看城市名称加上客户数量(汇总结果),因此阿瓜迪亚: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)

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


梵蒂冈之花
浏览 142回答 1
1回答

Cats萌萌

您可以创建一个列表,其中将包含您想要的内容,然后text=list在中进行设置data。也不要忘记指定hoverinfo='text'。我已经更新了您的代码,因此请尝试以下操作:import pandas as pdimport plotly.offline as pyodf = 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)  
随时随地看视频慕课网APP

相关分类

Python
我要回答