Python Plotly - 对纬度和经度进行地理编码 - 根据设施类型需要不同的符号

Python Plotly - geocoding latitude and longitude - want different symbols depending on type of facility

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

流行 |地点 |纬度 |经度 | f_type

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

如果您查看 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'])