""" Animate how a Gaussian curve changes as we change the parameter 's'. Recipe 3: This is the most advanced version, using a 'FuncAnimation' object from matplotlib. We define a function 'next_frame' which takes the s-value as input and creates updates the plot for the new frame. This function is passed as an argument to the 'FuncAnimation' class 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; #the midpoint of the curve (constant) #create an array of s-values, one for each curve/frame: 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()