我正在使用 matplotlib 计时器对象来注册我自己的动画更新函数。尽管不保留对计时器对象的引用,但一旦回调开始,我似乎就无法停止它们。
到目前为止,根据我的经验,当我在 matplotlib 中创建一个对象时,我会得到对它的引用,但它也会添加到其他对象(图中的轴、轴中的线等)内的列表中,这可以然后稍后再查询。但是我找不到计时器对象所在的位置。我的问题可以通过这个代码片段来总结import matplotlib.pyplot as plt
import numpy as np
def update():
plt.get_current_fig_manager().canvas.figure.patch.set_facecolor(str(np.random.random()))
plt.draw()
def start_animation():
timer = fig.canvas.new_timer(interval = 50)
timer.add_callback(update)
timer.start()
fig = plt.figure()
start_animation()
运行上面的代码片段,然后尝试以编程方式停止闪烁。需要调用的函数是
timer.remove_callback(update).
说清楚一点。我知道我可以保留对计时器对象的引用,这个问题就消失了。我正在寻找该对象必须位于 matplotlib 中何处的解释。
最佳答案
怎么样
self.timer = fig.canvas.new_timer(interval=100)
...
self.timer.remove_callback(...)
澄清引用是在 callafter 方法中。 您的图形或 Canvas 中没有存储任何引用 您可以在后端源代码中看到这一点
def new_timer(self, *args, **kwargs):
"""
Creates a new backend-specific subclass of :class:`backend_bases.Timer`.
This is useful for getting periodic events through the backend's native
event loop. Implemented only for backends with GUIs.
optional arguments:
*interval*
Timer interval in milliseconds
*callbacks*
Sequence of (func, args, kwargs) where func(*args, **kwargs) will
be executed by the timer every *interval*.
"""
return TimerTk(self._tkcanvas, *args, **kwargs)
它只是返回一个 TimerTK 实例。该引用继续存在,因为在 TimerTk.start()
方法中,您会看到一个 callafter 继续阻止计时器进行垃圾收集
class TimerTK(TimerBase):
...
def _timer_start(self):
self._timer_stop()
self._timer = self.parent.after(self._interval, self._on_timer)
这就是为什么每个示例都显示保存您自己对计时器的引用
关于python - matplotlib 中的计时器位于何处,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18197277/