""" This code will not show an animation, but will save an image file for each frame, which can later be turned into a movie by external software, for instance from the ImageMagick suite. """ 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) # we need a counter to create unique filenames: counter = 0 """ for loop over s values to update the y data, draw the plot and save it to file:""" for s in s_values: y = f(x,m,s) lines[0].set_ydata(y) plt.draw() plt.savefig('tmp_%04d.png' %(counter)) counter += 1 """ Terminal> python animate_gauss2.py (No plot is shown, but a number of files named "tmp_xxxx.png" should be created. These can be combined into a movie with external software.) """