""" Animate how a Gaussian curve changes as we change the parameter 's'. Recipe 1: Run the animation "live" by drawing each curve as it is created. """ import matplotlib.pyplot as plt import numpy as np 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 for s in s_values: y = f(x, m, s) lines[0].set_ydata(y) #update plot data and redraw plt.draw() plt.pause(0.1)