""" This code uses a FuncAnimation object from matplotlib to create the animation. The initialization of the plot is largely the same as the other recipes, but instead of a for-loop we define a function which updates the plot from one frame to the next. We then pass this function as an argument to the FuncAnimation object, which handles the loop to create the animation. """" 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; """ We will animate how the curve changes shape as s goes from 2 to 0.2, so we create an array of s values in this interval: """ 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) #define a function to update the plot from one frame to the next: def next_frame(frame): y = f(x, m, frame) lines[0].set_ydata(y) return lines """Create a FuncAnimation object to make the animation. The arguments are: plt.gcf(): the current active figure (gcf ->get current figure) next_frame: function which updates the plot from one frame to the next s_values: our array of s values interval: a time interval (in milliseconds) between each plot """ ani = FuncAnimation(plt.gcf(), next_frame, frames=s_values, interval=100) #save the animation as a movie (may require installing external software) ani.save('movie.mp4',fps=20) #show the animation, will loop by default plt.show() """ Terminal> python animate_gauss3.py (Output is an animation that will loop until the window is closed. If you don't want it to loop this can be set as an argument to the FuncAnimation object. A movie file is also created, but this may require installing additional external software that matplotlib will call.) """