【问题标题】:Matplotlib animation duration and why it won't stopMatplotlib 动画持续时间以及为什么它不会停止
【发布时间】:2019-07-07 15:57:12
【问题描述】:

matplotlib animation duration 解释说,基本上 FuncAnimation 中的参数 franes 定义了它应该动画的总帧数。但是,当我运行示例代码时,它似乎一直在运行。我预计无花果将在 4 秒后停止更新,但事实并非如此。我需要禁用某种循环吗?谢谢。我在 Python 3.7 和 matplotlib 3.0.3 上运行它

import numpy as np 
from matplotlib import pyplot as plt 
from matplotlib import animation

fig = plt.figure()
ax = plt.axes(xlim=(0, 2), ylim=(-2,2))
line, = ax.plot([], [], lw=2)

# init func, plot the background of each frame. 
def init():
    line.set_data([], [])
    return line, 

def animate(i):
    x = np.linspace(0, 2, 1000)
    y = np.sin(2 * np.pi * (x - 0.01 * i))
    line.set_data(x, y)
    return line, 

anim = animation.FuncAnimation(fig, animate, init_func=init, frames=200, interval=20, blit=True)
plt.show()

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

    您需要在FuncAnimation 呼叫中使用repeat=False

    请查看文档https://matplotlib.org/api/_as_gen/matplotlib.animation.FuncAnimation.html#matplotlib.animation.FuncAnimation

    repeat : bool, optional
    
        Controls whether the animation should repeat when the sequence of frames is completed. Defaults to True.
    

    【讨论】:

      【解决方案2】:

      默认情况下它会重复动画,但你可以使用

      FuncAnimation(... , repeat=False)
      

      文档:FuncAniamtion

      【讨论】:

        猜你喜欢
        • 2014-03-27
        • 2015-06-20
        • 1970-01-01
        • 2012-01-25
        • 1970-01-01
        • 1970-01-01
        • 2016-03-11
        • 2016-03-16
        • 2017-10-28
        相关资源
        最近更新 更多