python - matplotlib 中的计时器位于何处

标签 python matplotlib

我正在使用 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/

相关文章:

python - 从python中的IP地址获取响应

python - 张量来自不同的图

python - 在不加载内存的情况下随机播放大量项目

python - 绘图 matplotlib django 顶部的空白

matplotlib - Matplotlib 中相交透明层的正确图例颜色

python - 为每个 plt.step 行指定不同的颜色

python - 设置 WingIDE 以调试 Flask 项目

python - 使用 matplotlib 在网格中显示值

python - 使用 spines 在 matplotlib 中仅自定义刻度线的一侧

pandas - 如何在 matplot 上绘制散点趋势线? Python-Pandas