1 import matplotlib.animation as animation
2
3 n = 100
4 x = np.random.randn(n)
1 # create the function that will do the plotting, where curr is the current frame
2 def update(curr):
3 # check if animation is at the last frame, and if so, stop the animation a
4 if curr == n:
5 a.event_source.stop()
6 plt.cla()
7 bins = np.arange(-4, 4, 0.5)
#每次重复绘制直方图
8 plt.hist(x[:curr], bins=bins)
9 plt.axis([-4,4,0,30])
10 plt.gca().set_title('Sampling the Normal Distribution')
11 plt.gca().set_ylabel('Frequency')
12 plt.gca().set_xlabel('Value')
13 plt.annotate('n = {}'.format(curr), [3,27])
1 fig = plt.figure()
2 a = animation.FuncAnimation(fig, update, interval=100)
(讲道理图片会动)