Scatter Plot Animation - Trouble Adding Trailing Lines

Hi @chins,
Welcome to forum.
To animate the trendline of two scatter traces, mode='lines' you should define initial data and the frames as follows:

n_frames=30
y1 = #an array of shape (30,) or a dataframe column
y2 =#an array of shape(30,)
fig = go.Figure(data=[go.Scatter(x=[0], y=[y1[0]], mode='lines', line_color='blue'),
                               go.Scatter(x=[0], y=[y2[0]], mode='lines', line_color='red')])


frames = [go.Frame(data= [go.Scatter( #update trace0, i.e. fig.data[0]
                               x=np.arange(k+1),
                               y=y1[:k+1]),
                          go.Scatter(#updates trace1, i.e. fig.data[1]
                               x=np.arange(k+1),
                               y=y2[:k+1])],
                   traces=[0,1]# this line informs plotly.js that the first Scatter updates trace0, and the second, trace1
                   name=f'frame{k}'       
              )for k  in  range(1,n_traces)]   

A concrete example I posted here a while ago, as an answer to a similar question: https://community.plotly.com/t/cumulative-lines-animation-in-python/25707/2

3 Likes