#!/usr/bin/env python # script animate_wave.py ''' example of a simpe animation: running wave .. author:: Guenter Quast ''' # dependencies: PYTHON v2.7, numpy, matplotlib # last modified: #-------------------------------------------------------------- # pyhton2 - python3 compatibility from __future__ import division from __future__ import absolute_import from __future__ import print_function from __future__ import unicode_literals # import numpy as np import matplotlib.pyplot as plt import matplotlib.animation as anim import sys ##### ---- main Program starts here ----- if __name__ == "__main__": fig=plt.figure(figsize=(7.5,7.5)) ax=fig.add_subplot(1,1,1) ax.grid(True) ax.set_title('sin(kx - $\\omega$t)', size='xx-large') ax.set_xlabel('normalized position $x/\\lambda$') ax.set_ylabel('normalized amplitude $A / A_0$') x=np.linspace(0.,2.,200) nperiods=2 nrep=200 k,omega=2*np.pi,2*np.pi timetxt = ax.text(0.25, 0.93, ' ', transform=ax.transAxes,size='x-large', backgroundcolor='white') y=np.sin(k*x) graph, = ax.plot(x,y,'--', color='black') def animate(n): t=nperiods*float(n)/nrep y=np.sin(k*x - omega*t) graph, = ax.plot(x,y,'-', color='blue', lw=2) timestamp='t = %.2f $\\cdot$ T' %(t) timetxt.set_text(timestamp) return graph, timetxt print('\n*==* script ' + sys.argv[0]+' executing') ani=anim.FuncAnimation(fig, animate, nrep, interval=50, blit=True, init_func=None, fargs=None, repeat=True) plt.show()