如何将零点暂停()暂停?

时间:2015-11-19 20:03:07

标签: animation matplotlib draw show pause

我正在绘制一个圆圈动画。只要speed设置为正数,它看起来效果很好。但是,我想将speed设置为0.0。当我这样做时,某些东西会发生变化而不再是动画。相反,我必须点击' x'每帧后的窗口。我尝试使用plt.draw()plt.show()的组合来获得与plt.pause()相同的效果,但框架不会显示出来。如果没有涉及计时器或将其设置为plt.pause(),我如何精确复制0.0的功能?

speed = 0.0001
plt.ion()
for i in range(timesteps):
    fig, ax = plt.subplots()
    for j in range(num):
        circle = plt.Circle(a[j], b[j]), r[j], color='b')
        fig.gca().add_artist(circle)
    plt.pause(speed)
    #plt.draw()
    #plt.show()
    plt.clf()
    plt.close()

1 个答案:

答案 0 :(得分:1)

我在这里复制了pyplot.pause()的代码:

def pause(interval):
    """
    Pause for *interval* seconds.

    If there is an active figure it will be updated and displayed,
    and the GUI event loop will run during the pause.

    If there is no active figure, or if a non-interactive backend
    is in use, this executes time.sleep(interval).

    This can be used for crude animation. For more complex
    animation, see :mod:`matplotlib.animation`.

    This function is experimental; its behavior may be changed
    or extended in a future release.

    """
    backend = rcParams['backend']
    if backend in _interactive_bk:
        figManager = _pylab_helpers.Gcf.get_active()
        if figManager is not None:
            canvas = figManager.canvas
            canvas.draw()
            show(block=False)
            canvas.start_event_loop(interval)
            return

    # No on-screen figure is active, so sleep() is all we need.
    import time
    time.sleep(interval)

如您所见,它调用start_event_loop,它会在interval秒内启动一个单独的原始事件循环。如果interval == 0似乎是后端依赖的,会发生什么。例如,对于WX后端,值为0意味着此循环是阻塞的并且永远不会结束(我必须在此处查看the code,它不会显示在文档中。请参阅第773行。)

简而言之,0是一个特例。你不能把它设置为一个很小的值,例如0.1秒?

上面的pause文档字符串表示它只能用于粗暴的攻击,如果你想要更复杂的东西,你可能不得不诉诸animation module