【问题标题】:How do I stop an animation in matplotlib?如何在 matplotlib 中停止动画?
【发布时间】:2018-10-06 01:30:04
【问题描述】:

我是 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()  

就像我说的那样,我对 python 比较陌生,所以如果这是一个简单的答案,我真的很抱歉,但我到处寻找,但尝试过的方法似乎都不起作用,例如设置帧数并没有改变任何东西,所以我不知所措真的。

【问题讨论】:

  • 你试过ani.event_source.stop()吗?
  • 在哪里添加到代码中?
  • 我真的说不出来,因为我不知道你程序的逻辑。请在此处查看类似问题的解决方案:stackoverflow.com/questions/16732379/…

标签: python animation matplotlib


【解决方案1】:

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

ani = animation.FuncAnimation(fig, animate,frames=10, interval=1,repeat=False)

所以当你没有帧时它会停止。

【讨论】:

    猜你喜欢
    • 2016-03-16
    • 1970-01-01
    • 2013-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-14
    • 2020-09-16
    • 2015-03-26
    相关资源
    最近更新 更多