fig,(ax1,ax2)=plt.subplots(1,2)x=np.arange(0,2*np.pi,0.01)line1,=ax1.plot(x,np.sin(x))line2,=ax2.plot(x,np.cos(x))defanimate(i):line1.set_ydata(np.sin(x+i/10.0))line2.set_ydata(np.cos(x+i/10.0))returnline1,line2 ani=animation.FuncAnimation(fig,animate,frames=100...
下面是一个很基本的例子: """A simple example of an animated plot"""importnumpy as npfrommatplotlibimportpyplot as pltfrommatplotlibimportanimation#First set up the figure, the axis, and the plot element we want to animatefig =plt.figure()#create our line object which will be modified in th...
line,=plt.plot(x,y) # 动画函数 def animate(i): line.set_ydata(np.sin(x+i/100)) return line, # 动画初始函数 def init(): line.set_ydata(np.sin(x)) return line, """ fig:figure对象 animate:动画函数,不断更新图像的函数,生成新的xdata和ydata frames:动画的帧数 init_func=init:动画...
= ax.plot(x, np.sin(x))# 注意,这里line后面要加上逗号,表示一个具有一个元素的元组# print(type(line))# print(type((line,)))# <class 'matplotlib.lines.Line2D'># <class 'tuple'>defanimate(i):# 这里的i其实就是参数0-99,即时frames控制的参数,控制程序画图变换的次数# print(i) # 0-99...
line, = ax.plot([], [], lw=3) def init(): line.set_data([], []) return line, def animate(i): x = np.linspace(0, 4, 1000) y = np.sin(2 * np.pi * (x - 0.01 * i)) line.set_data(x, y) return line, anim = FuncAnimation(fig, animate, init_func=init, ...
(0,2*np.pi,0.01)# 创建x数据,用于绘制正弦波line,=ax.plot(x,np.sin(x))# 绘制初始正弦波# 创建动画ani=animation.FuncAnimation(fig=fig,func=lambdai:animate(i,line,x),# 指定动画函数frames=np.arange(1,200),# 设置动画帧数init_func=lambda:init(line,x),# 指定初始化函数interval=20,# ...
line, = ax.plot([], [], lw=2) # initialization function: plot the background of each frame def init(): line.set_data([], []) return line, # animation function. This is called sequentially # note: i is framenumber def animate(i): ...
line.set_data(x, y) return line, anim = FuncAnimation(fig, animate, init_func=init, frames=200, interval=20, blit=True) anim.save('sine_wave.gif', writer='imagemagick') · 在第7行到第9行,简单地创建一个图形窗口,图中只有一个轴。然后,创建无内容的行对象,其本质上是在动画中可修改的对...
line, = ax.plot([], [], lw=3) def init(): line.set_data([], []) return line, def animate(i): x = np.linspace(0, 4, 1000) y = np.sin(2 * np.pi * (x - 0.01 * i)) line.set_data(x, y) return line, anim = FuncAnimation(fig, animate, init_func=init, ...
animate 函数是刚刚写的由 FuncAnimation() 通过帧数来调用的函数(帧数参数会自动传入,不需要声明)。 frames 是动画最大帧数的限制,这里我们设置成 200 帧,也就是说 200 帧后动画会自动结束。 interval 是每两帧的间隔时间,单位为毫秒。这里我们用 50 毫秒。