Reputation: 15
I am trying to plot 2 choropleth maps side by side with two different columns: temperature (in degrees) and discount (in percentage). Here are the codes I am using
fig_1 = px.choropleth_mapbox(df, locations = 'code', geojson=municipalities_json, color = 'discount')
fig_1.update_geos(fitbounds = 'locations', visible = False)
fig_2 = px.choropleth_mapbox(df, locations = 'code', geojson=municipalities_json, color = 'est_ma_hightemp')
fig_2.update_geos(fitbounds = 'locations', visible = False)
fig = make_subplots(rows=1, cols=2,
subplot_titles=['Discounts', 'Weather'],
specs=[[{'type': 'choroplethmapbox'}, {'type': 'choroplethmapbox'}]])
fig.add_trace(fig_1['data'][0], row=1, col=1)
fig.add_trace(fig_2['data'][0], row=1, col=2)
fig.update_mapboxes(style='white-bg',
center={'lat': 37.3, 'lon': 127.6751},
zoom=5.8)
fig.update_layout(title_text='Title',
margin={'l': 0, 'r': 0, 't': 100, 'b': 0},
height=1000)
fig.show()
The figure has one color bar only. Obviously, this does not make any sense. Is there a way to use two different color bars?
Thanks!
Upvotes: 1
Views: 2571
Reputation: 35205
I don't have data for graphs or geojson data, so I don't know if it can be done with express. It is possible to achieve this using a graph object. This code is modified from the example answer in the plotly community to fit your code. Another thing to realize is that you need a free access token for the mapbox. The graph data and geojson data are from the official reference.
from plotly.subplots import make_subplots
import plotly.express as px
import plotly.graph_objects as go
mapboxtoken = open("mapbox_api_key.txt").read()
df = px.data.election()
geojson = px.data.election_geojson()
fig = make_subplots(
rows=1, cols=2, subplot_titles=['Discounts', 'Weather'],
specs=[[{"type": "mapbox"}, {"type": "mapbox"}]]
)
fig.add_trace(go.Choroplethmapbox(geojson=geojson,
locations=df['district'],
z=df['Coderre'],
featureidkey='properties.district',
colorscale='Viridis',
colorbar=dict(thickness=20, x=0.46),
marker=dict(opacity=0.75)), row=1, col=1)
fig.add_trace(go.Choroplethmapbox(geojson=geojson,
locations=df['district'],
z=df['Bergeron'],
featureidkey='properties.district',
colorscale='matter_r',
colorbar=dict(thickness=20, x=1.02),
marker=dict(opacity=0.75, line_width=0.5)), row=1, col=2)
fig.update_mapboxes(
bearing=0,
accesstoken=mapboxtoken,
center = {"lat": 45.5517 , "lon": -73.7073 },
)
fig.update_layout(margin=dict(l=0, r=0, t=50, b=10))
fig.update_layout(mapbox1=dict(zoom=8.5, style='carto-positron'),
mapbox2=dict(zoom=8.5, style='light'))
fig.show()
Upvotes: 2