animate time series python

Solutions on MaxInterview for animate time series python by the best coders in the world

showing results for - "animate time series python"
Ginny
20 Jan 2021
1import numpy as np
2import matplotlib.pyplot as plt
3from matplotlib import animation
4
5dt = 0.01
6tfinal = 1
7x0 = 0
8
9sqrtdt = np.sqrt(dt)
10n = int(tfinal/dt)
11xtraj = np.zeros(n+1, float)
12trange = np.linspace(start=0,stop=tfinal ,num=n+1)
13xtraj[0] = x0
14
15for i in range(n):
16    xtraj[i+1] = xtraj[i] + np.random.normal()
17
18x = trange
19y = xtraj
20
21# animation line plot example
22
23fig, ax = plt.subplots(1, 1, figsize = (6, 6))
24
25def animate(i):
26    ax.cla() # clear the previous image
27    ax.plot(x[:i], y[:i]) # plot the line
28    ax.set_xlim([x0, tfinal]) # fix the x axis
29    ax.set_ylim([1.1*np.min(y), 1.1*np.max(y)]) # fix the y axis
30
31anim = animation.FuncAnimation(fig, animate, frames = len(x) + 1, interval = 1, blit = False)
32plt.show()
33