Python: Choropleths updatemenus

Hi everybody,
I have been trying to build a choropleth map, where I can change the color/data of the choropleth by using buttons. The maps work with mapbox and till now I’ve been unable to get it working. The problem seems that the mapbox layers cannot be updated later on.

Happy for any suggestions!!

#import json
import plotly.plotly as py
import plotly.tools as tls
import plotly.graph_objs as go

#setup
tls.set_credentials_file(username=‘username’, api_key=‘your_plotly_key’)
mapbox_access_token = “MAPBOX-KEY”

source_red = ‘https://raw.githubusercontent.com/plotly/datasets/master/florida-red-data.json
source_blue = ‘https://raw.githubusercontent.com/plotly/datasets/master/florida-blue-data.json

data = go.Data([
go.Scattermapbox(
lat=[‘45.5017’],
lon=[’-73.5673’],
mode=‘markers’,
)
])
layout = go.Layout(
height=600,
autosize=True,
hovermode=‘closest’,
mapbox=dict(
layers=[
dict(
sourcetype = ‘geojson’,
source = source_red,
type = ‘fill’,
color = ‘rgba(163,22,19,0.8)’
)],
accesstoken=mapbox_access_token,
bearing=0,
center=dict(
lat=27.8,
lon=-83
),
pitch=0,
zoom=5.2,
style=‘light’
),
)

updatemenus=list([
dict(
buttons=list([
dict(
args=[‘mapbox.layers.source’, source_red],
label=‘red’,
method=‘relayout’
),
dict(
args=[‘mapbox.layers.source’, source_blue],
label=‘blue’,
method=‘relayout’
)
]),
direction = ‘left’,
pad = {‘r’: 10, ‘t’: 10},
showactive = True,
type = ‘buttons’,
x = 0.1,
xanchor = ‘left’,
y = 1.1,
yanchor = ‘top’
)
])

layout[‘updatemenus’] = updatemenus

fig = dict(data=data, layout=layout)
py.iplot(fig, filename=‘county-level-choropleths-python’)

Hey. It was a while ago, but did you ever figure out why updatemenus wasn’t updating map layers?
I have a similar issue where I’d like to have a map dropdown with basemap choices, one of which is a map layer of ESRI imagery.

Thanks

mapbox_layers=[
        {
            "below": 'traces',
            "sourcetype": "raster",
            "source": [
                "https://basemap.nationalmap.gov/arcgis/rest/services/USGSImageryOnly/MapServer/tile/{z}/{y}/{x}"
            ]
        }
        ]
   

Map_updatemenus=list([
    
    dict(
        buttons=list([
            
            dict(
                args=[{'mapbox.style': 'carto-positron'}],
                label='Light',
                method='relayout'
            ),
            
            dict(
                args=[{'mapbox.style': 'open-street-map'}],
                label='Street Map',
                method='relayout'
            ),                    
            
            dict(
                args=[{'mapbox.style': 'carto-darkmatter'}],
                label='Dark',
                method='relayout'
            ),
            
            dict(
                args=[{'mapbox.layers': mapbox_layers}],
                label='Aerial',
                method='relayout'
            )
        ])
1 Like

Hi, were you able to figure out how to solve this issue?