首页 > 解决方案 > (按 GeoJson 属性进行索引)如何使用正确的 featureidkey 绘制等值线?

问题描述

我正在关注 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 上,但是没有它,地图就不会绘制每个省份的多边形。

标签: pythongeojsonchoropleth

解决方案


TypeError:choropleth() 得到了一个意外的关键字参数“geojson”

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

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


推荐阅读