importnumpyasnpimportmatplotlib.pyplotaspltfrommatplotlib.animationimportFuncAnimation# Data & figurex=np.linspace(0,2*np.pi,400)fig,ax=plt.subplots()(line,)=ax.plot([],[],lw=2)# artist to updateax.set_xlim(0,2*np.pi)ax.set_ylim(-1.2,1.2)ax.grid(True)definit():line.set_data([],[])return(line,)# return a tuple/list of artistsdefupdate(frame):# frame is an int (0..N-1) or any object from your frames iterableprint(f"Frame {frame}")# for debuggingy=np.sin(x+0.05*frame)line.set_data(x,y)# mutate existing artistreturn(line,)anim=FuncAnimation(fig,update,init_func=init,frames=200,# or frames=range(200) or any iterable/generatorinterval=20,# milliseconds between framesblit=True# only re-draw changed artists (faster on many backends))plt.show()
Note
The callable update must return an iterable of artist when blit=True