0

How to plotted a choropleth using correctly featureidkey ?

CODE: 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: geoProvinces = json.load(response) df_new = ecuador.rename(columns={'Provinces': 'DPA_DESPRO'}) fig = px.choropleth_mapbox(df_new, geojson=geoProvinces, color=df_new['Confirmed deaths'], locations=df_new['DPA_DESPRO'], #featureidkey='properties.DPA_DESPRO', center={"lat": -2, "lon": -80}, mapbox_style="carto-positron", zoom=9 ) fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0}) fig.show() fig = px.choropleth_mapbox(df_new, geojson=geoProvinces, color=df_new['Confirmed deaths'], locations=df_new['DPA_DESPRO'], center={"lat": -2, "lon": -80}, mapbox_style="carto-positron", zoom=9 ) fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0}) fig.show() There is a missing parameter which is #featureidkey, I tried with featureidkey='properties.DPA_DESPRO' , but the choroplet maps is plotting the areas of each provinces and this gives me the followin error: TypeError: choropleth_mapbox() got an unexpected keyword argument 'featureidkey' I am folloing this link: https://plotly.com/JUMP_LINK__&&__python__&&__JUMP_LINK/choropleth-maps/ Please help me.

4th Nov 2020, 7:44 AM
Rosana Rodríguez Milanés
Rosana Rodríguez Milanés - avatar
2 Answers
+ 1
First update and install geopandas: !conda install -c conda-forge geopandas !pip install git+git://github.com/geopandas/geopandas.git import plotly.express as px # To plot choroplet maps import geopandas as gpd fig = px.choropleth(ecuador, geojson=geojson_ec, locations=ecuador['Provinces'], color_continuous_scale="OrRd", title="Covid cases in Ecuador", featureidkey="properties.dpa_despro", color=ecuador['Confirmed cases'], hover_name=ecuador['Provinces'], labels="properties.dpa_despro", scope='world', projection='natural earth' ) fig.update_geos( fitbounds="locations", visible=False) fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0}) fig.add_parcoords() fig.show()
5th Nov 2020, 7:05 PM
Rosana Rodríguez Milanés
Rosana Rodríguez Milanés - avatar
+ 1
First, the error says "TypeError: choropleth_mapbox() got an unexpected keyword argument 'featureidkey'", so you can't use featureidkey to plot the map. However, if you pay attention to the geojson structure of the first example at https://plotly.com/JUMP_LINK__&&__python__&&__JUMP_LINK/choropleth-maps/ you will see the json has 4 keys [type, properties, geometry, id] for each list element, meanwhile, in your ecuadorian geojson you will find only 3 keys [type,properties,geometry]. That being said, the solution is to add a fourth key "id" to your geojson, for every element in the list, and then run the code as is in the plotly example, then you will have no error.
15th Jun 2021, 10:25 PM
Danny Guevara
Danny Guevara - avatar