衰变
这个例子展示了:
- 使用生成器来驱动动画,
- 在动画期间更改轴限制。

import numpy as npimport matplotlib.pyplot as pltimport matplotlib.animation as animationdef data_gen(t=0):cnt = 0while cnt < 1000:cnt += 1t += 0.1yield t, np.sin(2*np.pi*t) * np.exp(-t/10.)def init():ax.set_ylim(-1.1, 1.1)ax.set_xlim(0, 10)del xdata[:]del ydata[:]line.set_data(xdata, ydata)return line,fig, ax = plt.subplots()line, = ax.plot([], [], lw=2)ax.grid()xdata, ydata = [], []def run(data):# update the datat, y = dataxdata.append(t)ydata.append(y)xmin, xmax = ax.get_xlim()if t >= xmax:ax.set_xlim(xmin, 2*xmax)ax.figure.canvas.draw()line.set_data(xdata, ydata)return line,ani = animation.FuncAnimation(fig, run, data_gen, blit=False, interval=10,repeat=False, init_func=init)plt.show()
