Subloting mapbox in jupyter notebook


#1

Hey everyone,
I’m trying to sublots 4 mapbox images. The goal would be to display 4 maps side-by-side, but I can’t find how to do it. Using append_trace will throw an error because Scattermapbox can’t contain xaxis property, and I haven’t seen any other information. Does anyone know how to do this / knows where I could find a great example of it ?
Thanks !
Quentin


#2

Hi @wats0ns,

You’re right that the make_subplots/append_trace approach doesn’t support mapbox traces at this point. Your best bet is to set the domain of each mapbox subplot directly. Here’s an example of putting two mapbox traces side by side

import plotly.plotly as py
import plotly.graph_objs as go

mapbox_access_token = 'pk.eyJ1Ijoiam1tZWFzZSIsImEiOiJjamljeWkwN3IwNjEyM3FtYTNweXV4YmV0In0.2zbgGCjbPTK7CToIg81kMw'

data = [
    go.Scattermapbox(
        lat=['45.5017'],
        lon=['-73.5673'],
        mode='markers',
        marker=dict(
            size=14
        ),
        text=['Montreal'],
        subplot='mapbox',
    ),
    go.Scattermapbox(
        lat=['45.5017'],
        lon=['-73.5673'],
        mode='markers',
        marker=dict(
            size=14
        ),
        text=['Montreal'],
        subplot='mapbox2',
    )
]

layout = go.Layout(
    autosize=True,
    hovermode='closest',
    mapbox=dict(
        accesstoken=mapbox_access_token,
        domain={'x': [0, 0.4], 'y': [0, 1]},
        bearing=0,
        center=dict(
            lat=45,
            lon=-73
        ),
        pitch=0,
        zoom=5
    ),
    mapbox2=dict(
        accesstoken=mapbox_access_token,
        domain={'x': [0.6, 1.0], 'y': [0, 1]},
        bearing=0,
        center=dict(
            lat=45,
            lon=-73
        ),
        pitch=0,
        zoom=5
    ),
)

fig = go.FigureWidget(data=data, layout=layout)
fig

The key properties to notice are the subplot property of the mapbox traces and the domain property of each mapbox subplot in the layout section. You can continue the pattern with mapbox3, mapbox4, etc.

Hope that helps!
-Jon


#3

Hey @jmmease, thanks for the quick answer, however this gives me the following error:
ValueError: Invalid property specified for object of type plotly.graph_objs.Layout: 'mapbox2'


#4

Try upgrading your version of plotly.py (I’m on 3.4.2).
-Jon


#5

That was it ! Do you know how I could name each of the subplot (like a title) ?


#6

There’s not a direct way to create subplot titles with this approach. What the make_subplots function does underneath is position annotations in paper coordinates as the titles. Here’s an example of doing this to create custom axis labels (https://plot.ly/python/text-and-annotations/#adding-annotations-with-xref-and-yref-as-paper). You could use the same approach for subplot titles, it will just take some fiddling to position the annotations where you want them.

Hope that helps!
-Jon