如何使用 Plotly 创建区级分区统计图

问题描述 投票:0回答:1

我有关于毛里求斯各地区的 geojson 数据,我想绘制一张分区统计图。

geojson文件的属性如下:

{'id_0': 143, 'iso': 'MUS', 'name_0': 'Mauritius', 'id_1': 1, 'name_1': 'Agalega', 'hasc_1': 'MU.AG', 'ccn_1': 0, 'cca_1': None, 'type_1': 'Region', 'engtype_1': 'Region', 'nl_name_1': None, 'varname_1': None}

{'id_0': 143, 'iso': 'MUS', 'name_0': 'Mauritius', 'id_1': 2, 'name_1': 'Black River', 'hasc_1': 'MU.BL', 'ccn_1': 0, 'cca_1': None, 'type_1': 'District', 'engtype_1': 'District', 'nl_name_1': None, 'varname_1': None}

{'id_0': 143, 'iso': 'MUS', 'name_0': 'Mauritius', 'id_1': 3, 'name_1': 'Flacq', 'hasc_1': 'MU.FL', 'ccn_1': 0, 'cca_1': None, 'type_1': 'District', 'engtype_1': 'District', 'nl_name_1': None, 'varname_1': None}

{'id_0': 143, 'iso': 'MUS', 'name_0': 'Mauritius', 'id_1': 4, 'name_1': 'Grand Port', 'hasc_1': 'MU.GP', 'ccn_1': 0, 'cca_1': None, 'type_1': 'District', 'engtype_1': 'District', 'nl_name_1': None, 'varname_1': None}

{'id_0': 143, 'iso': 'MUS', 'name_0': 'Mauritius', 'id_1': 5, 'name_1': 'Moka', 'hasc_1': 'MU.MO', 'ccn_1': 0, 'cca_1': None, 'type_1': 'District', 'engtype_1': 'District', 'nl_name_1': None, 'varname_1': None}

{'id_0': 143, 'iso': 'MUS', 'name_0': 'Mauritius', 'id_1': 6, 'name_1': 'Pamplemousses', 'hasc_1': 'MU.PA', 'ccn_1': 0, 'cca_1': None, 'type_1': 'District', 'engtype_1': 'District', 'nl_name_1': None, 'varname_1': None}

{'id_0': 143, 'iso': 'MUS', 'name_0': 'Mauritius', 'id_1': 7, 'name_1': 'Plaines Wilhems', 'hasc_1': 'MU.PW', 'ccn_1': 0, 'cca_1': None, 'type_1': 'District', 'engtype_1': 'District', 'nl_name_1': None, 'varname_1': None}

{'id_0': 143, 'iso': 'MUS', 'name_0': 'Mauritius', 'id_1': 8, 'name_1': 'Port Louis', 'hasc_1': 'MU.PL', 'ccn_1': 0, 'cca_1': None, 'type_1': 'District', 'engtype_1': 'District', 'nl_name_1': None, 'varname_1': None}

{'id_0': 143, 'iso': 'MUS', 'name_0': 'Mauritius', 'id_1': 9, 'name_1': 'Riviere du Rempart', 'hasc_1': 'MU.RR', 'ccn_1': 0, 'cca_1': None, 'type_1': 'District', 'engtype_1': 'District', 'nl_name_1': None, 'varname_1': None}

{'id_0': 143, 'iso': 'MUS', 'name_0': 'Mauritius', 'id_1': 10, 'name_1': 'Rodrigues', 'hasc_1': 'MU.RO', 'ccn_1': 0, 'cca_1': None, 'type_1': 'Autonomous island', 'engtype_1': 'Autonomous island', 'nl_name_1': None, 'varname_1': 'Rodrigues'}

{'id_0': 143, 'iso': 'MUS', 'name_0': 'Mauritius', 'id_1': 11, 'name_1': 'Saint Brandon', 'hasc_1': 'MU.CC', 'ccn_1': 0, 'cca_1': None, 'type_1': 'Region', 'engtype_1': 'Region', 'nl_name_1': None, 'varname_1': 'Cargados Carajos)'}

{'id_0': 143, 'iso': 'MUS', 'name_0': 'Mauritius', 'id_1': 12, 'name_1': 'Savanne', 'hasc_1': 'MU.SA', 'ccn_1': 0, 'cca_1': None, 'type_1': 'District', 'engtype_1': 'District', 'nl_name_1': None, 'varname_1': None}

要绘制的数据位于

LocationJobCount.csv
文件中,制表符用作分隔符:

             Location  JobCount
0                Moka        55
1          Port Louis        45
2       Pamplemousses        13
3             Savanne         3
4  Riviere du Rempart         8
5         Black River         6
6     Plaines Wilhems       200

位置列包含地区名称。

我的代码

"""
Python : 3.9.7
Plotly : 5.8.0
"""
import pandas as pd
import json
import plotly.express as px

districts = json.load(open("stanford-ph377fn8728-geojson.json", 'r'))
df = pd.read_csv("LocationJobCount.csv", sep='\t')

# map each location in dataframe to location in geojson
district_id_map = {}
for feature in districts['features']:
    district_id_map[feature['properties']['name_1']] = feature['properties']['id_1']
df['id'] = df['Location'].apply(lambda x: district_id_map[x])

# create choropleth map
fig = px.choropleth(df, geojson=districts,
                    locations=df['id'],
                    color='JobCount',
                    color_continuous_scale="algae",
                    range_color=[0, max(df['JobCount'])],
                    labels={"Value": "Count"}
                    )
fig.update_layout(geo_scope="world", geo_resolution=50)
# fig.update_geos(fitbounds="locations", visible=False)
fig.show()

在 geojson 和源文件之间映射位置后,

df
看起来像这样:

             Location  JobCount  id
0                Moka        55   5
1          Port Louis        45   8
2       Pamplemousses        13   6
3             Savanne         3  12
4  Riviere du Rempart         8   9
5         Black River         6   2
6     Plaines Wilhems       200   7

结果

放大到毛里求斯后,我看到了这个:

没有看到任何地区。

我尝试了什么

  • 参考文献 1:我阅读了这篇文章并确保 geojson 文件中的地区拼写与数据框中的拼写相匹配。
  • 取消注释
    fig.update_geos(fitbounds="locations", visible=False)
    会输出白屏。
  • Reference2:添加
    locationmode='country names'
    作为参数不会执行任何操作。
  • 参考3
    fig_map.update_layout(geo_scope="africa", geo_resolution=50)
    不起作用,因为毛里求斯岛不在这个范围内。

我的目标是创建类似的东西:

python plotly plotly-python choropleth
1个回答
1
投票

我之前的代码中的问题可能是由于错误的 GeoJSON 对象/数据文件配对造成的。

解决方案

就我而言,我可以直接将 GeoJSON 文件中的

name_1
属性与数据框中的
Location
列配对。更多信息:https://plotly.com/python/choropleth-maps/#indexing-by-geojson-properties

import pandas as pd
import json
import plotly.express as px
import plotly.io as pio
pio.renderers.default = 'browser'  # to show geojson map in web browser

districts = json.load(open("stanford-ph377fn8728-geojson.json", 'r'))
df = pd.read_csv("LocationJobCount.csv", sep='\t')

fig = px.choropleth(df, geojson = districts,
                    featureidkey='properties.name_1',
                    locations='Location',  # column in dataframe which contains districts names
                    color='JobCount',  # data from this column in dataframe is plotted
                    color_continuous_scale="algae",
                    )
fig.update_geos(fitbounds="locations") 
fig.show()

这有效,但地图上缺少

JobCount
为 0 的一些地区。

我修改了我的数据框以包含所有地区并将 0 设置为默认值:

Location    JobCount
Black River 6
Flacq   0
Grand Port  0
Moka    60
Pamplemousses   17
Port Louis  53
Riviere du Rempart  10
Savanne 3
Plaines Wilhems 233

© www.soinside.com 2019 - 2024. All rights reserved.