Blank chorpleth map with Plotly express

I am trying to build a choropleth map in Plotly of Regions in England by ‘NUTS code’ from data supplied by the government at: https://data.gov.uk/dataset/f884db1c-38a1-4fa1-9d0e-3c4a0d80e2da/nuts-level-3-january-2018-full-clipped-boundaries-in-the-united-kingdom

My pandas df contains house price data from each region and looks as such:

    NUTS code                      Region name  ...     price
0          UK                   United Kingdom  ...  1268454.0
1         UK0                          England  ...  1093785.0
2         UKC                       North East  ...    41145.0
3        UKC1           Tees Valley and Durham  ...    18154.0
4       UKC11  Hartlepool and Stockton-on-Tees  ...     4457.0

I am following the docs: https://plotly.com/python/mapbox-county-choropleth/ which has a geojson file with ~3000 features of US states.

The Plotly code is followed from the examples as looks as such:

import plotly.express as px

fig = px.choropleth_mapbox(df, geojson=spatial['features'], 
                       locations='NUTS code', color='price', 
                       featureidkey='properties.nuts318cd',
                       color_continuous_scale="Viridis",
                       mapbox_style="carto-positron",
                       zoom=5, center = {"lat": 51.4816, "lon": 3.1791},
                       opacity=0.5,
                       labels={'price':'Price (£)'})

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

However this simply bring ups a blank World Map with no filling or geometry of the UK regions. Does anyone know why this is?

Thanks

@geds133,
To be recognized by mapbox, your geojson file must have the following definition:

spatial ={'type': 'FeatureCollection',
                   'features': []
                  }

Defining

geojson=spatial['features']

mapbox cannot identify the type and does not render the choropleth. To get it pass

geojson=spatial

to px.choropleth_mapbox.

1 Like

Thanks @empet.

Although this works in a local environment, when on Dash it still does not load any data. Is there a way to clip the mapbox boundary so that it only renders the UK and not the whole world?

I believe this may help.

@geds133,
Unfortunately you cannot clip the boundary on a mapbox map.

1 Like