""" Animation recipe 3. This is the most advanced approach, using the FuncAnimation tool from matplotlib to create the plot. It supports both viewing the plot and creating a movie file directly, but creating the movie file still depends on external software (ImageMagick). """ 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 #function to update the plot, the core of the FuncAnimation approach: 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=50) ani.save('movie.mp4',fps = 20) #this line may not work unless you have installed additional software plt.show()