""" Animate how a Gaussian curve changes as we change the parameter 's'. Recipe 2: Nothing is shown on the screen, but one file is saved for each frame. These can be turned into a movie later using other software. """ import numpy as np import matplotlib.pyplot as plt 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 frame_counter = 0 for s in s_values: y = f(x, m, s) lines[0].set_ydata(y) #update plot data and redraw plt.draw() plt.savefig(f'tmp_{frame_counter:04d}.png') #unique filename frame_counter += 1