互换的青春
为了使您的示例起作用,您必须更改两件事:从某处存储返回值FuncAnimation。否则你的动画会在plt.show().如果不想画线而只想画点,请plt.plot使用animationfrom matplotlib.animation import FuncAnimationimport matplotlib.pyplot as pltimport numpy as npfig, ax = plt.subplots()ax.set_xlim(0,2)ax.set_ylim(0,2)line, = plt.plot(0,0,'bo')def animation(i): x=np.linspace(0,2,100) y=np.linspace(0,1,100) plt.plot(x[i],y[i],'bo') return line,my_animation=FuncAnimation(fig, animation, frames=np.arange(100),interval=10)plt.show()如果你只想在图表上有一个移动点,你必须设置并从inblit=True返回结果:plot.plotanimationfrom matplotlib.animation import FuncAnimationimport matplotlib.pyplot as pltimport numpy as npfig, ax = plt.subplots()ax.set_xlim(0,2)ax.set_ylim(0,2)line, = plt.plot(0,0,'bo')def animation(i): x=np.linspace(0,2,100) y=np.linspace(0,1,100) return plt.plot(x[i],y[i],'bo')my_animation=FuncAnimation( fig, animation, frames=np.arange(100), interval=10, blit=True)plt.show()此外,您可能想摆脱 (0,0) 处的点,并且不想为每个动画帧计算xand :yfrom matplotlib.animation import FuncAnimationimport matplotlib.pyplot as pltimport numpy as npfig, ax = plt.subplots()ax.set_xlim(0,2) ax.set_ylim(0,2) x=np.linspace(0,2,100) y=np.linspace(0,1,100) def animation(i): return plt.plot(x[i], y[i], 'bo')my_animation=FuncAnimation( fig, animation, frames=np.arange(100), interval=10, blit=True)plt.show()