Adding Multiple Layers in Mapbox

I would like to add multiple layers to my plot in Mapbox using Python. Suppose, I would like to add another layer which is a scatter plot. How and where would I add it?

Also, how do I rearrange the layers so the scatter plot is above the geojson?

Thanks a lot!

Here’s my code:

data = [
go.Scattermapbox(
    lat=['39.91427','38.91538','38.91458',
         '39.92239','40.93222','38.90842',
         '40.91931','40.93260','38.91368',
         '39.88516','39.921894','38.93206',
         '39.91275'],
    lon=['-75.02827','-75.02013','-77.03155',
         '-75.04227','-75.02854','-77.02419',
         '-75.02518','-75.03304','-77.04509',
         '-75.99656','-75.042438','-77.02821',
         '-75.01239'],
    mode='markers',
    marker=go.scattermapbox.Marker(
        size=9
    ),
)
]

layout = go.Layout(
height=600,
autosize=True,
hovermode='closest',
mapbox=dict(
    layers=[            dict(
            sourcetype = 'geojson',
            source = 'new_export/S_FRD_Pol_Ar.json',
            type = 'fill',
            color = 'rgba(172, 206, 222, 1)'
        ),
        dict(
            sourcetype = 'geojson',
            source = 'new_export/S_CSLF_Ar.json',
            type = 'fill',
            color = 'rgba(255, 237, 67, 1)'
        )
    ],
    accesstoken=mapbox_access_token,
    bearing=0,
    center=dict(
        lat=40,
        lon=-75
    ),
    pitch=0,
    zoom=10,
    style='light'
),
)

fig = go.Figure(layout=layout, data=data)
iplot(fig, filename='Multiple Mapbox')
1 Like

So I’m not too sure regarding your first question with the multiple layers but I think you’d just throw it into data

data = [
go.Scattermapbox(
            'First chunk'
    ),
go.Scatter(
              'And the 2nd chunck
   )
]

For the arranging layer levels you can use the below property. so could set below = 'water' on one layer and the other would appear on top. Works with most mapbox layer properties.

I have the same question. It is possible to change the layers of a choropleth map with dropdown-menus? I know how to change the data but i am not able to change the colors of the layer. So even if the dropdown and data is changed i have always the same layer with the same colors in every county.

@bd317

Today (Aug 7, 2019) it was released plotly 4.1.0, that provides a new chart type - Choroplethmapbox.
Now it’s simpler to plot a mapbox choropleth and it works fine with a dropdown menu.
I experimented it here https://plot.ly/~empet/15237
I generated synthetic z-data. If you read them from a file, you can get z_min, and z_max values, among all z-values read for each case, and set in each Choroplethmapbox definition the same zmin =z_min, zmax=z_max.

1 Like

Some more resources on these new chart types & features:

Many thanks to the organization that sponsored this feature :heart: (more about how to sponsor & fund our open source work: https://plot.ly/products/consulting-and-oem).

2 Likes

@empet

Thanks for your great tutorials. I followed your first tutorial about choropleth maps to create my own maps!

But in your example you use the same colorscale for every year. I wanted to use different colors for the layers. Yesterday i found the solution finally:

I created a list with the different layers:

lay_list = [layer1,layer2…]

And I passed the list to the arg arguments with the following statement:

{‘mapbox.layers’: lay_list[s]}],

So i can you use different, individual created layers for each entry of the dropdown menu.

Best Wishes

1 Like

none of the above is a good answer to the question. These are all creative workarounds but none produces the desired results of overlayed maps.

2 Likes