""" Recipe 3 for movies: Use a FuncAnimation from Matplotlib, to both run live and save to a movie file. """ import numpy as np import matplotlib.pyplot as plt from matplotlib.animation import FuncAnimation def f(x, m, s): return (1.0/(np.sqrt(2*np.pi)*s))*np.exp(-0.5*((x-m)/s)**2) m = 0; s_start = 2; s_stop = 0.2 s_values = np.linspace(s_start,s_stop,30) x = np.linspace(-3*s_start,3*s_start, 1000) max_f = f(m,m,s_stop) plt.axis([x[0],x[-1],0,max_f]) plt.xlabel('x') plt.ylabel('y') y = f(x,m,s_start) lines = plt.plot(x,y) #initial plot to create the lines object def next_frame(frame): y = f(x, m, frame) lines[0].set_ydata(y) return lines ani = FuncAnimation(plt.gcf(), next_frame, frames=s_values, interval=100) ani.save('movie.mp4',fps=20) plt.show()