""" This code will run a matplotlib animation "live", i.e. simply draw the plots on the screen in sequence, with a short pause between to make the movie look right. """ import numpy as np import matplotlib.pyplot as plt #the Gauss function def f(x, m, s): return (1.0/(np.sqrt(2*np.pi)*s))*np.exp(-0.5*((x-m)/s)**2) m = 0 #midpoint of curve """ 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,20) x = np.linspace(-3*s_start,3*s_start,100) #calculate the highest value of f and fix the axes f_max = f(m,m,s_stop) plt.axis([-3*s_start,3*s_start,0,f_max]) y = f(x,m,s_start) """ We want to update the plot, so we keep the object return from plt.plot in a variable:""" lines = plt.plot(x,y) #for loop over s values to update the y data and draw the plot for s in s_values: y = f(x,m,s) lines[0].set_ydata(y) plt.draw() plt.pause(0.2) """ Terminal> python animate_gauss1.py (output is an animation that will run once, and then the plotting window is closed.) """