# Python: Choropleths updatemenus

**URL:** <https://community.plotly.com/t/python-choropleths-updatemenus/23567>\
**Category:** 📊 Plotly Python\
**Created:** [May 17, 2019, 8:57am UTC](https://community.plotly.com/t/python-choropleths-updatemenus/23567 "2019-05-17T08:57:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rabel](https://avatars.discourse-cdn.com/v4/letter/r/9dc877/32.png) [@rabel](https://community.plotly.com/u/rabel)\
**Post date:** [May 17, 2019, 8:57am UTC](https://community.plotly.com/t/python-choropleths-updatemenus/23567/1 "2019-05-17T08:57:14Z")

</div>

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](https://raw.githubusercontent.com/plotly/datasets/master/florida-red-data.json)’  
source\_blue = ‘[https://raw.githubusercontent.com/plotly/datasets/master/florida-blue-data.json](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’)

---

<div class="post-metadata">

**Author:** ![bruzzy](https://avatars.discourse-cdn.com/v4/letter/b/6f9a4e/32.png) [@bruzzy](https://community.plotly.com/u/bruzzy)\
**Post date:** [April 13, 2020, 1:08pm UTC](https://community.plotly.com/t/python-choropleths-updatemenus/23567/2 "2020-04-13T13:08:23Z")

</div>

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

```auto
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'
            )
        ])

```

---

<div class="post-metadata">

**Author:** ![Pra713](https://avatars.discourse-cdn.com/v4/letter/p/85e7bf/32.png) [@Pra713](https://community.plotly.com/u/Pra713)\
**Post date:** [March 9, 2024, 3:38am UTC](https://community.plotly.com/t/python-choropleths-updatemenus/23567/3 "2024-03-09T03:38:25Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jam878](https://avatars.discourse-cdn.com/v4/letter/j/9de053/32.png) [@jam878](https://community.plotly.com/u/jam878)\
**Post date:** [August 19, 2024, 9:02pm UTC](https://community.plotly.com/t/python-choropleths-updatemenus/23567/4 "2024-08-19T21:02:18Z")

</div>

Hi, I was having the exact same issue as you were describing and was able to get it working!

My solution was to update both the mapbox.layers and mapbox.style arguments simultaneously. This should work for the other map styles as well—hope this helps!

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

fig.update_layout(
    updatemenus=[
        dict(
            buttons=list([
                dict(
                    args=[{"mapbox.layers": []},
                          {"mapbox.style": "open-street-map"}
                          ],
                    label="OpenStreetMap",
                    method='relayout'
                ),
                dict(
                    args=[{"mapbox.layers": mapbox_layers},
                          {"mapbox.style": 'white-bg'}
                          ],
                    label="Satellite",
                    method='relayout'
                )
            ])
        )
    ]
)

```
