Hello! Iām using plotly 4.9.0 for python, and Iām trying to add a time slider to a scattergeo map. I have a running example of the map without the slider, and a running example of a slider with a scatter graph, but the combination of scattergeo + slider fails. I only manage to make it run the first time, and then it gets stuck on the last frame when I try to re-play it, or move the slider around. Could somebody please point me to what am I doing wrong? Thanks a lot!
Here is the simplified code, with the option for scattergeo commented (you can easily switch from scatter to scattergeo in the example):
# Data scatter
lines = pd.DataFrame({'x1': [0,1,2,3],
'x2': [2,3,4,5],
'y1': [0,1,2,3],
'y2': [1,2,3,4],
})
# Data geo
coords = pd.DataFrame({'msa_name': ['Anchorage, AK',
'Daphne-Fairhope-Foley, AL',
'Mobile, AL',
'Prescott, AZ'],
'lat': [62.212784, 30.659218, 30.971509, 34.540000],
'lon': [-149.678220, -87.746067, -88.207870, -112.468500]})
data = []
frames = []
sliders_dict = {
'yanchor': 'top',
'xanchor': 'left',
'currentvalue': {
'font': {'size': 20},
'prefix': 'Year:',
'visible': True,
'xanchor': 'right'
},
'transition': {'duration': 300, 'easing': 'cubic-in-out'},
'pad': {'b': 10, 't': 50},
'len': 0.9,
'x': 0.1,
'y': 0,
'steps': []
}
for i in np.arange(4):
# Create snapshot
data_dict = go.Scatter(x = [lines.loc[i, 'x1'], lines.loc[i, 'x2']],
y = [lines.loc[i, 'y1'], lines.loc[i, 'y2']],
mode = 'lines',
)
#data_dict = go.Scattergeo(
# locationmode = 'USA-states',
# lon = [df.loc[i*100,'INTPTLONG']],
# lat = [df.loc[i*100,'INTPTLAT']],
# mode = 'markers',
# name = str(i),
#)
# Add snapshot to data
if (i == 0):
data.append(data_dict)
# Create frame
frame = {'data': [data_dict], 'name': str(i)}
frames.append(frame)
# Create slider step
slider_step = {'args': [[str(i)],
{'frame': {'duration': 300, 'redraw': False},
'mode': 'immediate',
'transition': {'duration': 300}
}
],
'label': str(i),
'method': 'animate'}
sliders_dict['steps'].append(slider_step)
# Layout
lay = go.Layout(xaxis = {'range':[0,5]},
yaxis = {'range':[0,5]},
autosize = True,
hovermode = 'closest',
geo = dict(scope='usa',
projection=dict(type='albers usa'),
showland = True,
landcolor = landcolor,
subunitwidth=1,
countrywidth=1,
subunitcolor='white',
countrycolor='white',
showlakes=True,
lakecolor='white',
resolution=50),
showlegend = False,
width = 800,
height = 500,
margin = go.layout.Margin(l=10, r=10, b=10, t=10, pad=0),
updatemenus = [{'buttons': [{'args': [None,
{'frame': {'duration': 500, 'redraw': False},
'fromcurrent': True,
'mode':'immediate',
'transition': {'duration': 300, 'easing': 'quadratic-in-out'}}],
'label': 'Play',
'method': 'animate'
},
{'args': [[None],
{'frame': {'duration': 0, 'redraw': False},
'mode': 'immediate',
'transition': {'duration': 0}}],
'label': 'Pause',
'method': 'animate'
}
],
'direction': 'left',
'pad': {'r': 10, 't': 87},
'showactive': False,
'type': 'buttons',
'x': 0.1,
'xanchor': 'right',
'y': 0,
'yanchor': 'top'
}],
sliders = [sliders_dict]
)
figure = go.Figure(data=data, layout=lay, frames=frames)
plotly.offline.iplot(figure)