(通过GeoJson属性进行索引)如何使用正确的featureidkey绘制choropleth?

2024-06-01 07:51:51 发布

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

下面是URL的示例:https://plotly.com/python/choropleth-maps/#base-map-configuration按GeoJSON属性编制索引)。这是我的代码:

厄瓜多尔的数据框架:

Provinces    Confirmed cases    Confirmed deaths    Probable deaths
0   AZUAY      10688.0                 195.0          12.0
1   BOLÍVAR    2115.0                  66.0           12.0
2   CAÑAR       2153.0                 83.0           7.0
3   CARCHI      3058.0                 104.0          1.0
4   CHIMBORAZO  2536.0                 315.0         119.0

map_ecuador = folium.Map(location=[-1.3397668, -79.3666965.], tiles='OpenStreetMap', zoom_start=7)

geoURL="https://data.humdata.org/dataset/e66dbc70-17fe-4230-b9d6-855d192fc05c/resource/6fa37b41-ad28-40a6-9641-3b4efd4dbe13/download/ecuador.geojson"
    with urlopen(geoURL) as response:
        geojson = json.load(response)
print(ecuador["Provinces"][0])
print(geojson["features"][0]["properties"])

results:
AZUAY
{'DPA_VALOR': 0, 'DPA_ANIO': '2011', 'DPA_CANTON': '0101', 'DPA_DESCAN': 'CUENCA', 'DPA_PROVIN': 
'01', 'DPA_DESPRO': 'AZUAY', 'PCODE2': 'EC0101'}


fig = px.choropleth(ecuador, geojson=geojson, color="Bergeron",
                    locations=ecuador['Provinces'], 
                    featureidkey="features.properties",
                    projection="mercator"
                   )
fig.update_geos(fitbounds="locations", visible=False)
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
fig.show()

然后,我得到了这个错误:

TypeError                                 Traceback (most recent call last)
<ipython-input-68-7439eb1d4543> in <module>()
      2                     locations=ecuador['Provinces'],
      3                     featureidkey="features.properties",
----> 4                     projection="mercator"
      5                    )
      6 fig.update_geos(fitbounds="locations", visible=False)

TypeError: choropleth() got an unexpected keyword argument 'geojson'

请帮我检查一下我正在遵循的指南。我认为我的问题在于featureidkey,但如果没有它,地图不会绘制每个省份的多边形


Tags: httpsmapgeojsonfigupdatepropertiesfeatureslocations
1条回答
网友
1楼 · 发布于 2024-06-01 07:51:51

TypeError: choropleth() got an unexpected keyword argument 'geojson'

该错误消息告诉您问题出在哪里:您正在传递一个geojson参数,这是它不期望的。您使用的plotly版本是什么?在v4.5.0中geojson属性被添加到了px.choropleth。我怀疑你使用的是旧版本

https://github.com/plotly/plotly.py/blob/master/CHANGELOG.md#450 -2020-01-22

相关问题 更多 >