Hi @yumoto
To answer your second question, the difference between Choroplethmap
and Choroplethmapbox
is that the latter does not use MapLibre tiles, while the former does.
‘mapbox’ suffix is replaced by ‘map’ in plotly==5.24.0
see;
mapbox to maplibre
I believe that Mapbox will eventually be deprecated.
I recently changed from Choropleth
to Choroplethmap
for a few visualizations because I wanted to capture and use user interactions, such as clickData
and relayoutData
. You can overlay tiles in both but the latter allows for more interaction. The disadvantage to this is that it’s hard to get a truly minimal basemap, carto-positron-nolabels
will still show arterial roads etc… My workaround was to create a custom tile using MapTiler and remove literally everything!
Here’s some code I adapted from a project that shows the main difference (both are maps of France)
Choropleth:
import plotly.graph_objects as go
# Assume `df` is a GeoDataFrame and 'data_column' contains the data to visualize
# Example: `df['population_density']` as the data column
# Step 1: Initialize a basic Plotly figure
fig = go.Figure()
# Step 2: Add the choropleth map trace
fig.add_trace(
go.Choropleth(
geojson=df.__geo_interface__, # GeoJSON representation of the dataframe
z=df['data_column'], # Generic data to be visualized (e.g., population, density)
locations=df.index, # Locations mapped via the index of the dataframe
colorscale='Blues',
colorbar_title='Data',
marker_line_width=0.5,
marker_line_color='darkgray',
hovertemplate='<b>Location:</b> %{customdata[0]}<br><b>Data:</b> %{z}<extra></extra>', # Simple hover template
customdata=df[['region']].values # Extra data for the hovertemplate
)
)
# Step 3: Set default map center (e.g., France) and zoom level
avg_lat, avg_lon = 46.603354, 1.888334
zoom_level = 6
# Step 4: Update layout, set map boundaries, and center it
fig.update_layout(
geo=dict(
scope='europe', # Scope set to Europe
resolution=50, # Resolution of map boundaries
showcoastlines=False,
showland=True,
landcolor="lightgray",
center=dict(lat=avg_lat, lon=avg_lon), # Center the map on France
projection_scale=zoom_level # Set zoom level
),
margin=dict(l=10, r=10, t=30, b=10) # Adjust margins for less white space
)
# Step 5: Show the figure
fig.show()
Choroplethmap:
import plotly.graph_objects as go
# Assume `df` is a GeoDataFrame and 'data_column' contains the data to visualize
# Example: `df['population_density']` as the data column
# Step 1: Initialize a basic Plotly figure
fig = go.Figure()
# Step 2: Add a choropleth map trace to the figure
fig.add_trace(
go.Choroplethmap(
geojson=df.__geo_interface__, # GeoJSON representation of the GeoDataFrame
z=df['data_column'], # The data you want to visualize, e.g., population density
locations=df.index, # Map locations to the index of the dataframe
colorscale='Blues', # Use a basic colormap
colorbar_title='Data', # Title for the color bar
marker_line_width=0.5, # Thin line for boundaries
marker_line_color='darkgray', # Boundary color
hovertemplate='<b>Location:</b> %{customdata[0]}<br><b>Data:</b> %{z}<extra></extra>', # Hover info template
customdata=df[['region']].values # Extra data to display in the hovertemplate
)
)
# Step 3: Add a scatter text trace to display labels
centroids = df.geometry.centroid # Calculate centroids for label placement
fig.add_trace(
go.Scattermap(
lon=centroids.x, # Longitude of the centroids
lat=centroids.y, # Latitude of the centroids
text=df['region'], # Text labels, e.g., regions or departments
mode='text', # Display as text
textposition='top center', # Position the text
showlegend=False # No legend for text labels
)
)
# Step 4: Set map layout with centering and zoom
fig.update_layout(
map=dict(
style="../assets/basicTileMap.json", # Basic tile map style - the custom one created on MapTiler
center={'lat': 46.603354, 'lon': 1.888334}, # Center the map on France
zoom=6 # Set zoom level
),
margin=dict(l=10, r=10, t=30, b=10), # Adjust margins
height=700 # Set figure height
)
# Step 5: Show the figure
fig.show()
If you want to see the difference live, check out Michelin Guide to France. The ‘region’ plot is Choropleth
the ‘economic health’ plot is Choroplethmap
.
Good luck with your project,
pineapple-bois