1""" 2===== 3Decay 4===== 5 6This example showcases: 7- using a generator to drive an animation, 8- changing axes limits during an animation. 9""" 10 11import numpy as np 12import matplotlib.pyplot as plt 13import matplotlib.animation as animation 14 15 16def data_gen(t=0): 17 cnt = 0 18 while cnt < 1000: 19 cnt += 1 20 t += 0.1 21 yield t, np.sin(2*np.pi*t) * np.exp(-t/10.) 22 23 24def init(): 25 ax.set_ylim(-1.1, 1.1) 26 ax.set_xlim(0, 10) 27 del xdata[:] 28 del ydata[:] 29 line.set_data(xdata, ydata) 30 return line, 31 32fig, ax = plt.subplots() 33line, = ax.plot([], [], lw=2) 34ax.grid() 35xdata, ydata = [], [] 36 37 38def run(data): 39 # update the data 40 t, y = data 41 xdata.append(t) 42 ydata.append(y) 43 xmin, xmax = ax.get_xlim() 44 45 if t >= xmax: 46 ax.set_xlim(xmin, 2*xmax) 47 ax.figure.canvas.draw() 48 line.set_data(xdata, ydata) 49 50 return line, 51 52ani = animation.FuncAnimation(fig, run, data_gen, blit=False, interval=10, 53 repeat=False, init_func=init) 54plt.show() 55