Black Lives Matter. Please consider donating to Black Girls Code today.
Dash Enterprise delivers an incredible 21x cost savings 💸Download the e-book!

Choropleth map dutch townships

Hi,

I am trying to plot Covid-19 data on a plotly chorolepth map but the map does not show anything but an empty ploy. The geosjon file is as follows:
https://www.webuildinternet.com/articles/2015-07-19-geojson-data-of-the-netherlands/townships.geojson
And a Dataframe example looks as follows:

                     Gemeente  ...  Overleden_inc100000
0    's-Gravenhage (gemeente)  ...                  0,5

Below the code I used:

import pandas as pd
from urllib.request import urlopen, Request
import json

import plotly.express as px
#https://www.cbs.nl/nl-nl/onze-diensten/open-data/statline-als-open-data/cartografie
#https://chart-studio.plotly.com/~empet/15238/tips-to-extract-data-from-a-geojson-di/#/


#geodata_url1 = 'https://geodata.nationaalgeoregister.nl/cbsgebiedsindelingen/wfs?request=GetFeature&service=WFS&version=2.0.0&typeName=cbs_gemeente_2017_gegeneraliseerd&outputFormat=json'
geodata_url2 = 'https://www.webuildinternet.com/articles/2015-07-19-geojson-data-of-the-netherlands/townships.geojson'


req = Request(geodata_url2, headers={'User-Agent': 'Mozilla/5.0'})

with urlopen(req) as response:
    dutch_town = json.load(response)

df = pd.read_csv('C:/Users/Dante/Desktop/Covid-19/Covid_Data/Reported COVID-19 patients - Per county from 1-Jul-2020 until 14-Jul-2020.csv', delimiter=';')

#print(dutch_town)

fig = px.choropleth(df, geojson=dutch_town,
                    locations= 'Gemeente', color= "Totaal_Absoluut",
                    color_continuous_scale="Viridis",
                    range_color=(df["Totaal_Absoluut"].min(), df["Totaal_Absoluut"].max()),
                    labels={"Totaal_Absoluut": 'Overleden'}
                    )

fig.update_geos(fitbounds="locations", visible=False)
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
fig.show()

Hi, @DantevdH,
Most likely, your choropleth is empty because you did not pass the right information that connects the
data from your dataframe to that from the geojson.
Inspecting the geojson file, read via:

import json
with open('townships.geojson', 'r') as fp:
    jdata = json.load( fp)

it can be seen that each feature in the list jdata['features'] has a key, 'properties'.
For example

print(jdata['features'][0]['properties'])

lists:

{'code': '1680',
 'name': 'Aa en Hunze',
 'regioFacet': 'tcm:106-353398-1024',
 'level': 4,
 'url': '/regioinformatie/gemeente/aa-en-hunze/'}

If each feature is uniquely identified by a 'code' (here '1680'), then your dataframe should have a
column that records these code(s) (if df['Gemeente'] contains these codes it’s OK.

Moreover in the definition:

fig = px.choropleth()

pass:

featureidkey ='properties.code'

too.

print(help(go.Choropleth))

reveals that

featureidkey
 |          Sets the key in GeoJSON features which is used as id to
 |          match the items included in the `locations` array. Only
 |          has an effect when `geojson` is set. Support nested
 |          property, for example "properties.name".