My approach to creating a choropleth map via plotly
seems pretty straightforward--load in the DataFrame, load in the geojson, assign the necessary features to the custom polygons, and plot.
Obviously there is a missed step somewhere when referencing of the custom polygons as only a blank map appears after a lengthy loading time.
One major thing to note is that about half of the polygons are located within states, but are their own custom polygon within the states. Therefore to my knowledge, choropleth_mapbox
is the more suitable solution.
A sample of the image, showing custom polygons within the states:
The code:
import pandas as pd
import plotly.express as px
import geopandas as gpd
from geojson import Polygon
import json
# reading in the dataframe
path = '/path/to/csv'
df = pd.read_csv(path)
geo_df = gpd.GeoDataFrame(df)
# reading in the geospatial data
with open('/path/to/geojson') as f:
geojson = json.load(f)
# create the plot
fig = px.choropleth_mapbox(geo_df[0:50], #slicing for quick loading
geojson=geojson,
color="MALL",
locations="MWS_ID",
featureidkey="properties.MWS_ID",
center={"lat": 39,
"lon": -95},
mapbox_style="carto-positron",
zoom=3)
fig.show()
The output:
Obviously data is missing.
I think the issue is in the geojson file. The only thing I can see that might be off about the geojson structure is that the coordinates of my geojson are in two brackets, whereas the geojson from the documentation's coordinates are in three brackets.
Below is my geojson.
{'type': 'FeatureCollection',
'features': [{'type': 'Feature',
'geometry': {'type': 'Polygon',
'coordinates': [[-89.299965, 36.508405],
[-89.414355, 36.499866],
[-89.424498, 36.476321],
.....
'properties': {'MWS_ID': 'TN_1'}},
{'type': 'Feature',
'geometry': {'type': 'Polygon',
'coordinates': [[-111.043999, 44.139903],
[-111.040171, 42.227952],
[-111.040773, 41.820698],
.....
And below is the documentation's geojson
{'type': 'FeatureCollection',
'features': [{'type': 'Feature',
'geometry': {'type': 'MultiPolygon',
'coordinates': [[[[-73.6363215300962, 45.5759177646435],
[-73.6362833815582, 45.5758266113331],
.....
[-73.6363215300962, 45.5759177646435]]],
[[[-73.6561004885273, 45.5841347974261],
.....
[-73.6561004885273, 45.5841347974261]]]]},
'properties': {'district': '11-Sault-au-Récollet'},
'id': '11'},
{'type': 'Feature',
'geometry': {'type': 'Polygon',
'coordinates': [[[-73.6217484540132, 45.5544783077209],
[-73.6235005117779, 45.5536358848324],
[-73.6278096771011, 45.5513024018691],
Here is how I created the geojson
# Create the GeoJSON
names_merged = names_1 + names_2
geoms_merged = geoms_1 + geoms_2
geojson = {'type':'FeatureCollection', 'features':[]}
for i in range(len(names_merged)):
feature = Feature(geometry=geoms_merged[i])
geojson['features'].append(feature)
geojson['features'][i]['properties']['MWS_ID'] = names_merged[i]
with open('/path/to/geojson', 'w') as f:
dump(geojson, f)
Where names_merged is a list of MWS_IDs in str
format and geoms_merged is a list of polygons in the geojson.geometry.Polygon
format.
Verifying the dataframe and geojson have the same keys.
print(geo_df['MWS_ID'][3])
print(geojson["features"][28]['properties']["MWS_ID"])
The output
AZ_2
AZ_2
But the map is still blank.
Thank you for your continued help S.O. community.