Hi there,
Iβm using plotly to create an interactive timeline and have run into the following issue:
When using animation frames similar to the example here I am unable to update both the data and the layout (i.e. xaxis range) in the same frame.
I know that the data in the frame is correct because if I pause the visual it will show the correct data and axis range. However, when I click play again, the data begins to update while the layout axis range does not.
I have attached my code below, any help would be appreciated as I have been pulling my hair out for days trying to figure this out.
figure = {
'data':[{'x':[df['Date']], 'y':[df['High']], 'mode':'lines'}],
'layout':{
'title':'blah',
'font' : {'family' : 'Courier New, monospace', 'color' : '#FFF'},
'plot_bgcolor' : '#000',
'paper_bgcolor' : '#000',
'hovermode' : 'closest',
'hoverdistance' : 50,
'dragmode' : 'pan',
'sliders': {
'args' : ['transition', {'duration':0, 'easing':'linear'}],
'initialValue':'1/1/2015',
'plotlycommand':'animate',
'values': df['Date'],
'visible':True
},
'updatemenus' : [{
'buttons': [
{
'args' : [None, {'frame': {'duration':1, 'redraw': False},
'fromcurrent':True, 'transition':{'duration':0, 'easing':'linear'}}],
'label':'Play',
'method':'animate'
},
{
'args' : [[None], {'frame': {'duration':0, 'redraw': False},
'mode':'immediate', 'transition':{'duration':0}}],
'label':'Pause',
'method':'animate'
}
],
'direction': 'left',
'showactive':False,
'type': 'buttons',
'xanchor':'right',
'yanchor':'top',
'x':0,
'y':0
}],
'xaxis' : {
'title' : 'Date',
'titlefont' : {'color' : 'white'},
'tickcolor' : '#FFF',
'tickfont' : {'color':'white'},
'showgrid' : False,
'zeroline' : False,
'type' : 'date'
},
'yaxis' : {
'title' : 'blah',
'titlefont' : {'color':'white'},
'tickcolor' : '#FFF',
'tickfont' : {'color':'white'},
'range':[-5,5]
}
},
'frames':[]
}
sliders_dict = {
'active': 0,
'yanchor': 'top',
'xanchor': 'left',
'currentvalue': {
'font': {'size': 20},
'prefix': 'Date:',
'visible': True,
'xanchor': 'right'
},
'transition': {'duration': 0, 'easing': 'linear'},
'pad': {'b': 10, 't': 50},
'len': 0.9,
'x': 0.1,
'y': 0,
'steps': []
}
for ii in range(len(df)):
if ii < 90:
days = ii
else:
days = 90
data_by_month = df.iloc[ii-days:ii]
data_dict = {
'x' : list(data_by_month['Date']),
'y' : list(data_by_month['High']),
'mode':'lines',
'fillcolor':'#000',
'marker': {'color':'#FFF'},
'text':'Shooting Incident'
}
frame = {'data':[data_dict], 'layout':{'xaxis': {'range':[pd.to_datetime(df['Date'][ii-(days)]), pd.to_datetime(df['Date'][ii] + pd.Timedelta(days=45))], 'type':'date', 'autorange':False}}, 'name':str(df['Date'][ii])}
figure['frames'].append(frame)
slider_step = {
'args': [[str(df['Date'][ii])], {'frame': {'duration':0, 'redraw':False},
'mode':'immediate', 'transition': {'duration':0}}],
'label':str(df['Date'][ii]),
'method':'animate'
}
sliders_dict['steps'].append(slider_step)
figure['layout']['sliders'] = [sliders_dict]