gpt4 book ai didi

animation - 如何为零时间 pyplot pause()?

转载 作者:行者123 更新时间:2023-12-02 01:31:39 29 4
gpt4 key购买 nike

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

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

最佳答案

我在这里复制了 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如果您想要更复杂的东西。

关于animation - 如何为零时间 pyplot pause()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33813332/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com