Note
Go to the endto download the full example code.
Decay#
This example showcases:
using a generator to drive an animation,
changing axes limits during an animation.
Output generated viamatplotlib.animation.Animation.to_jshtml.
importitertoolsimportmatplotlib.pyplotaspltimportnumpyasnpimportmatplotlib.animationasanimationdefdata_gen():forcntinitertools.count():t=cnt/10yieldt,np.sin(2*np.pi*t)*np.exp(-t/10.)definit():ax.set_ylim(-1.1,1.1)ax.set_xlim(0,1)delxdata[:]delydata[:]line.set_data(xdata,ydata)returnline,fig,ax=plt.subplots()line,=ax.plot([],[],lw=2)ax.grid()xdata,ydata=[],[]defrun(data):# update the datat,y=dataxdata.append(t)ydata.append(y)xmin,xmax=ax.get_xlim()ift>=xmax:ax.set_xlim(xmin,2*xmax)ax.figure.canvas.draw()line.set_data(xdata,ydata)returnline,# Only save last 100 frames, but run foreverani=animation.FuncAnimation(fig,run,data_gen,interval=100,init_func=init,save_count=100)plt.show()
Total running time of the script: (0 minutes 11.571 seconds)