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’)