如何在 matplotlib 中停止动画?

inv*_*ble 2 python animation matplotlib

所以我是Python新手,一直在尝试用matplotlib中的动画来模拟地球和月球的运动,动画效果很好,但它永远不会真正结束,有没有一种简单的方法可以做到这一点?代码如下:

def animate(i):
    orbits()
    ax1.clear()
    plt.axis('equal')
    ax1.plot(xMlist, yMlist)
    ax2.plot(xElist, yElist)

ani = animation.FuncAnimation(fig, animate,frames=10, interval=1)
plt.show()  
Run Code Online (Sandbox Code Playgroud)

就像我说的,我对Python相对陌生,所以如果这是一个简单的答案,我真的很抱歉,但我到处都看了,尝试过的方法似乎都不起作用,例如设置帧数并没有改变任何东西,所以我真的很茫然。

ani*_*in4 5

您可以将重复参数设置为 false: https ://matplotlib.org/api/_as_gen/matplotlib.animation.FuncAnimation.html

ani = animation.FuncAnimation(fig, animate,frames=10, interval=1,repeat=False)
Run Code Online (Sandbox Code Playgroud)

所以当你超出框架时它就会停止。