如何使用Python中自己的数据创建美国各州和DC的Choropleth地图?

2024-04-19 13:46:41 发布

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

我有一个数据框架,其中包含美国各州和每个州的一些数字。我有哥伦比亚特区和它的数据,我需要在地图上画出来。这张地图描绘的是一个没有哥伦比亚特区的州

plotly.graph_objects..Figure(data=go.Choropleth(
locations=df['statecode'],
z=df['buyers'].astype(float),
locationmode='USA-states',
colorscale='Reds',
autocolorscale=False,
text=df['mytext'], 
marker_line_color='white',
colorbar_title="Total buyers"

))

它有基本上是州和县的FIP,它创建了县地图

    fips = USA_df['FIPS'].tolist()
    values = df['buyers'].tolist()

    plotly.figure_factory..create_choropleth(
    fips=fips, values=values,
    binning_endpoints=endpts,
    colorscale=colorscale,
    show_state_data=True,
    show_hover=True, centroid_marker={'opacity': 0},
    asp=2.9, title='',
    legend_title='Buyers'
)

所以,如果我使用stateside,则绘图仪将显示包含50个州的地图,并且排除DC。如果我使用fips,则会绘出县和区的地图

有没有办法为所有州和特区创建Choropleth美国地图


Tags: 数据dfdatatitleshow地图plotlymarker