import plotly.express as px
import plotly.graph_objects as go
import geopandas as gpd
df = px.data.election()
geojson = px.data.election_geojson()
gdf = (
gpd.GeoDataFrame.from_features(geojson)
.merge(df, on="district")
.assign(lat=lambda d: d.geometry.centroid.y, lon=lambda d: d.geometry.centroid.x)
.set_index("district", drop=False)
)
# for convenience of rebuilding and adding traces...
def basemap():
fig = px.choropleth_mapbox(
df,
geojson=geojson,
color="Bergeron",
locations="district",
featureidkey="properties.district",
center={"lat": 45.5517, "lon": -73.7073},
mapbox_style=mapboxstyle,
# mapbox_style="carto-positron",
zoom=9,
)
fig.update_layout(margin={"r": 0, "t": 0, "l": 0, "b": 0},
mapbox={"accesstoken":mapboxtoken}
)
return fig