Python绘图地理编码纬度和经度需要不同的符号,这取决于设施的类型

2024-06-28 20:00:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在对一个设施列表进行地理编码,我想通过它们是医院还是诊所来表示输出。我希望医院以正方形出现,诊所以圆形出现。我可以通过只映射一个来让我的Plotly map正常工作,但我无法确定如何让它按设备类型绘制不同的符号。我从一个包含人口(pop)、设施位置(location)、纬度(lat)、经度(lon)和设施类型(f_类型)的数据集导入。我的数据集如下所示:

pop | location | lat | lon | f|u型

20 |俄亥俄州克利夫兰| 41.4993 |-81.6944 |医院

感谢您的帮助

import plotly.graph_objects as go
from plotly.offline import plot
from plotly.subplots import make_subplots
import plotly.graph_objects as go

import pandas as pd

df = pd.read_excel(r'D:\python code\data mgmt\listforgeorural.xlsx')
df.head()

fig = go.Figure(data=go.Scattergeo(
        locationmode = 'USA-states',
        lon = df['lon'],
        lat = df['lat'],
        f_type = df['f_type'],
      
        text = df['location']+'<br>Number of Projects:'+ df['f_type'].astype(str),
        mode = 'markers',
        marker = dict(
            size = 17,
            opacity = 0.9,
            reversescale = False,
            autocolorscale = False,
            symbol = {['square', 'circle']},
            line = dict(
                width=1,
                color='rgba(102, 102, 102)'
            ),
            colorscale = 'Blues',
            cmin = 0,
            color = df['pop'],
            cmax = df['pop'].max(),
            colorbar_title="Number of Rural Projects: 2015 - 2020"
        )))

fig.update_layout(
        title = 'List of Rural Projects by Location of Project Lead/PI',
        geo = dict(
            scope='usa',
            projection_type='albers usa',
            showland = True,
            landcolor = "rgb(222, 222, 222)",
            subunitcolor = "rgb(255, 255, 255)",
            countrycolor = "rgb(217, 217, 217)",
            countrywidth = 0.5,
            subunitwidth = 0.5
        ),
    )

fig.show()
plot(fig, filename='output.html')

Tags: ofimportgo类型dfastypefig
1条回答
网友
1楼 · 发布于 2024-06-28 20:00:47

如果您查看Scattergeo的文档,特别是marker选项,它会说该选项中的symbol变量可以是一维数组或列表

因此,您只需要编写一个函数,将df['f_type']的所有元素转换为适当的符号。我已经为您完成了这项工作,如下所示:

def ftypesToSymbols(ftypes):
    option1 = 'square'       # Feel free to change this to any of the options available 
    option2 = 'circle'       # (see above)
    
    symbols = []
    for ftype in ftypes:
        if ftype == 'hospital':
            symbols.append(option1)
        else:                # ftype is clinic
            symbols.append(option2)
            
    return symbols

然后,只需将marker字典选项中的symbol变量设置为:
symbol = fTypesToSymbols(df['f_type'])

相关问题 更多 >