✊🏿 Black Lives Matter. Please consider donating to Black Girls Code today.
🐇 Announcing Dash VTK for 3d simulation graphics. Check out the March webinar.

Px.choropleth_mapbox using custom JSON file

I’m trying to use a custom JSON file http://brenocon.com/geocode/cbsa.tiger2013.json.gz, for the following plotly express chart:


fig = px.choropleth_mapbox(df, geojson=MSA_json, locations='CBSA', color='Employment',
                           color_continuous_scale="Viridis",
                           range_color=(0, 12),
                           mapbox_style="carto-positron",
                           zoom=3, center = {"lat": 37.0902, "lon": -95.7129},
                           opacity=0.5,
                           labels={'Employment':'Employment By Industry'}
                          )

However, it seems to run infinitely. I think I’m reading in my json wrong since it has , i’m using:


with open(r'C:\Users\Andrew\Documents\PythonScripts\US Data Dashboard\cbsa.tiger2013.json') as json_file:
    MSA_json = json.load(json_file)

Appreciate any help, thanks!

Hi @ilemi,

I was able to read your geojson file in short time:

import json
with open('cbsa_tiger2013.json', 'r') as fp:
    MSAjson = json.load(fp)

Its definition is as follows:

MSAjson.keys()
# displays:
#dict_keys(['type', 'features'])

MSAjson['features'][0].keys()
# displayed keys:
#dict_keys(['type', 'properties', 'geometry'])

MSAjson['features'][0]['properties']

{'CSAFP': '462',
 'CBSAFP': '40340',
 'GEOID': '40340',
 'NAME': 'Rochester, MN',
 'NAMELSAD': 'Rochester, MN Metro Area',
 'LSAD': 'M1',
 'MEMI': '1',
 'MTFCC': 'G3110',
 'ALAND': 6415950878.0,
 'AWATER': 74793561.0,
 'INTPTLAT': '+43.9499166',
 'INTPTLON': '-092.3356986'}

To get your choroplethmapbox you must select a key in feat['properties'] that uniquely identifies the
feat['geometry'] for each feat in the list MSAjson['features'].
Let us say that GEOID in feat[‘properties’] is this key.

Then based on the knowlege of this identifier you must define a data frame that associate to each
region labeled by its GEOID a value (employment percent, for example) and these values are recorded in the dataframe column df[‘CBSA’]. Then

your fig definition is as follows:

fig = px.choropleth_mapbox(df, geojson=MSA_json, 
                           locations='CBSA', 
                           featureidkey='properties.GEOID',
                           color='Employment',
                           color_continuous_scale="Viridis",
                           range_color=(0, 12),
                           mapbox_style="carto-positron",
                           zoom=3, center = {"lat": 37.0902, "lon": -95.7129},
                           opacity=0.5,
                           labels={'Employment':'Employment By Industry'}
                          )

Your initial code doesn’t work because the featureidkey is not set.

Note that you have to decide whether GEOID can be the id of a feat['geometry'] or not.
I just gave an example on how you must define the elements for a
px.choropleth_mapbox

1 Like

Ah you’re a lifesaver! Thank you empet!