1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| import matplotlib.animation as animation import matplotlib.pyplot as plt import numpy as np
X = np.arange(0, 10, 0.01) Ys = [np.sin(X + k / 10) for k in range(100)]
def my_ani(x, ys): fig, ax = plt.subplots() ax.set_title('y = sin(x + k/10)') ax.set_xlim([0, 10]), ax.set_xlabel('X') ax.set_ylim([-1, 1]), ax.set_ylabel('Y')
line, = ax.plot(x, ys[0]) ano = plt.annotate('k: 0', (1, 1))
def animate(i): line.set_ydata(ys[i]) ano.set_text('k: %d' % i) return line,
ani = animation.FuncAnimation(fig, animate, frames=30, interval=50) plt.show()
my_ani(X, Ys)
|