Reputation: 363
I'm looking to plot French election results using the Plotly express chloropleth map plot.
To do this, I used a dataset for the results (stable link) and another for the GeoJSON data of France territory (stable link).
Here is the code:
import geopandas as gpd
import pandas as pd
import plotly.express as px
t1 = pd.read_csv('p2022-resultats-departement-t1.csv')
geo = gpd.read_file('a-dep2021.json')
px.choropleth_mapbox(t1,
locations='CodeDépartement',
geojson=geo,
mapbox_style='carto-positron',
center={'lat': 47.7516, 'lon': 1.6751},
zoom=4).update_layout(margin={'l': 10, 'r': 10, 't': 10, 'b': 10})
But the plot I'm obtaining has holes in it:
My first thought about this issue was maybe a problem in the GeoJSON file data, but I check it with mapshaper and everything looks fine.
I also went through each dataset and there was no missing data for those regions.
Any ideas?
Upvotes: 1
Views: 286
Reputation: 35275
I think the map is missing because the column information in the data frame and the key information and association in the geo file are missing. Also, if you tie the color settings to a specific column, you can draw a color-coded map.
import geopandas as gpd
import pandas as pd
import plotly.express as px
t1 = pd.read_csv('./data/p2022-resultats-departement-t1.csv')
geo = gpd.read_file('./data/a-dep2021.json')
fig = px.choropleth_mapbox(t1,
locations='CodeDépartement',
geojson=geo,
color='MACRON.exp',
featureidkey="properties.dep",
mapbox_style='carto-positron',
center={'lat': 47.7516, 'lon': 1.6751},
zoom=4
)
fig.update_layout(margin={'l': 10, 'r': 10, 't': 10, 'b': 10})
fig.show()
Upvotes: 2