【问题标题】:How can I deactivate 'Warning: Source ID 510 was not found when attempting to remove it - GLib.source_remove(self._idle_event_id)'?如何停用“警告:尝试删除时未找到源 ID 510 - GLib.source_remove(self._idle_event_id)”?
【发布时间】:2015-04-09 14:07:27
【问题描述】:

当我执行时

#!/usr/bin/env python

import matplotlib.pyplot as plt

plt.plot([1, 2, 3, 4])
plt.show()

(以及更复杂的例子)我明白了

/usr/local/lib/python3.4/dist-packages/
matplotlib/backends/backend_gtk3.py:215: Warning: 
Source ID 7 was not found when attempting to remove it
    GLib.source_remove(self._idle_event_id)

是什么原因造成的,我该如何消除这些警告?

我知道我可以使用

import warnings
warnings.simplefilter("ignore")

摆脱所有警告,但这不是我所要求的。我想要警告,但 matplotlib 没有警告(尤其是上面的警告)。

【问题讨论】:

标签: python matplotlib gtk glib


【解决方案1】:

GLib.source_remove 不成功,因为之前执行的self.close_event() 可能已经完成了这项工作。

This commit 应该可以解决您的问题。它是从 2 月 23 日开始的。您可以等待下一个版本或手动应用补丁。

【讨论】:

【解决方案2】:

使用plt.close() 解决此问题。

【讨论】:

    【解决方案3】:

    很抱歉回答了一个老问题,但我在运行 Linux 发行版的机器上通过 pip 安装 Python 3.6.9 和 matplotlib 后遇到了类似的问题。我的意图是能够在所述机器上升级 Python 后重新运行涉及pyplot 的旧脚本。虽然脚本一直运行到完成并提供了预期的输出,但我总是收到以下警告:

    /home/jefgrailet/.local/lib/python3.6/site-packages/matplotlib/backends/backend_gtk3.py:195: Warning: Source ID 8 was not found when attempting to remove it
      GLib.source_remove(self._idle_draw_id)
    

    在使用pyplot 中的savefig() 方法时(我猜show() 方法也会出现类似的问题)。警告中提到的行对应于backend_gtk3.py中的这个方法:

    def destroy(self):
        #Gtk.DrawingArea.destroy(self)
        self.close_event()
        if self._idle_draw_id != 0:
            GLib.source_remove(self._idle_draw_id)
    

    我查看了matplotlib GitHub 以检查是否存在相同脚本的更新版本或者是否已知此问题,结果发现上述方法的current implementation 仅依赖于self.close_event()指令,即GLib.source_remove() 是不必要的。

    因此,我在上面的代码中注释了最后两行并保存了更改。 进行此编辑后,我可以运行我的脚本而不会收到任何警告。我希望这能帮助遇到类似问题的人。

    【讨论】:

    • 你说得对,我在使用来自pyplotsavefig 时遇到了同样的问题。但是,show 方法没有产生此错误。只有savefig 方法可以。不确定是否已针对此错误实施了一个好的解决方案(而不是不得不破解这些库的源代码)?
    • 我在使用 Python 3.8.2 和 matplotlib 3.2.1 时遇到了与 @CuriousLeo 相同的问题。
    • 未来自己的注意事项:请参阅下面@user1959279 的回答,以使用 matplotlib3.2.1 解决此问题
    猜你喜欢
    • 2014-06-05
    • 2021-11-05
    • 2016-09-05
    • 1970-01-01
    • 2014-11-08
    • 1970-01-01
    • 2021-03-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多