Search code examples
pythonplotlychoroplethplotly-express

plotly choroplethmapbox doesn't let me subplot


Using different sources, I wrote this code for subplotting two Colombian maps with a summarized quantity, on which I want to display on one side the log10 of the other, but it only works when I put both columns (col=1) on the same value (1). Here's the code:

locs = dfd['department']
for loc in counties['features']:
    loc['id'] = loc['properties']['NOMBRE_DPT']

fig = make_subplots(
    rows=1, cols=2, subplot_titles=['Normal distribution', 'Logarithm 10'],
    specs=[[{"type": "mapbox"}, {"type": "mapbox"}]]
)

fig.add_trace(go.Choroplethmapbox(
                    geojson=counties,
                    locations=dfd['department'],
                    z=dfd['cases'],
                    colorbar_title = 'First',
                    colorbar=dict(thickness=20, x=0.46),
                    marker=dict(opacity=0.75)), row=1, col=1)

fig.add_trace(go.Choroplethmapbox(
                    geojson=counties,
                    locations=dfd['department'],
                    z=np.log10(dfd['cases']),
                    colorbar_title = 'Second',
                    colorbar=dict(thickness=20, x=1.02),
                    marker=dict(opacity=0.75)), row=1, col=1)

fig.update_layout(margin=dict(l=20, r=0, t=40, b=40))

fig.update_layout(mapbox1=dict(zoom=3.4, style='carto-positron'),
                  mapbox2=dict(zoom=3.4, style='light'),
                  mapbox_center = {"lat": 4.570868, "lon": -74.2973328})

fig.show()

And a photo of what it is showing: enter image description here

Thanks!!!


Solution

  • Since you did not present any data, I have created a subplot of the examples in the reference, modified to fit your code. I think you need to configure the mapbox settings for each map as to why both are not showing up. I also need to set up an additional API token for mapbox as it is required in my environment. See here for details.

    from urllib.request import urlopen
    import json
    with urlopen('https://raw.githubusercontent.com/plotly/datasets/master/geojson-counties-fips.json') as response:
        counties = json.load(response)
    
    import pandas as pd
    import numpy as np
    
    df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/fips-unemp-16.csv", dtype={"fips": str})
    
    from plotly.subplots import make_subplots
    import plotly.graph_objects as go
    
    token = open("mapbox_api_key.txt").read()
    
    fig = make_subplots(
        rows=1, cols=2, subplot_titles=['Normal distribution', 'Logarithm 10'],
        specs=[[{"type": "mapbox"}, {"type": "mapbox"}]]
    )
    
    fig.add_trace(go.Choroplethmapbox(
        geojson=counties,
        locations=df.fips,
        z=df.unemp,
        colorscale="Viridis",
        colorbar_title = 'First',
        colorbar=dict(thickness=20, x=0.46),
        marker=dict(opacity=0.75)), row=1, col=1)
    
    fig.add_trace(go.Choroplethmapbox(
        geojson=counties,
        locations=df.fips,
        z=np.log10(df.unemp),
        colorscale="Viridis",
        colorbar_title = 'Second',
        colorbar=dict(thickness=20, x=1.02),
        marker=dict(opacity=0.75)), row=1, col=2)
    
    fig.update_layout(margin=dict(l=20, r=0, t=40, b=40))
    
    fig.update_layout(mapbox1=dict(zoom=3.4, style='carto-positron', center={"lat": 37.0902, "lon": -95.7129}, accesstoken=token),
                      mapbox2=dict(zoom=3.4, style='light', center={"lat": 37.0902, "lon": -95.7129}, accesstoken=token))
    
    fig.show()
    

    enter image description here