How can I deactivate 'Warning: Source ID 510 was not found when attempting to remove it - GLib.source_remove(self._idle_event_id)'?
Asked Answered
J

3

11

When I execute

#!/usr/bin/env python

import matplotlib.pyplot as plt

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

(and more complex examples) I get

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

What causes this and how can I get rid of these warnings?

I know that I can use

import warnings
warnings.simplefilter("ignore")

to get rid of all warnings, but that is not what I am asking for. I want to have warnings, but none from matplotlib (especially the one from above).

Jarredjarrell answered 9/4, 2015 at 14:7 Comment(2)
I'm getting the same problem in github.com/nltk/nltk/issues/942Dworman
I can't reproduce the issue with Python 3.8 / matplotlib 3.2.2Jarredjarrell
G
11

GLib.source_remove was not successful because the self.close_event() that was executed before probably already did the job.

This commit should fix your problem. It is from the 23rd February. You can either wait for the next release or apply the patch manually.

Gragg answered 18/4, 2015 at 13:14 Comment(1)
Note: the commit that fixed this issue came a few days after the release of v1.4.3 (released on 2015-02-16 github.com/matplotlib/matplotlib/releases/tag/v1.4.3), as of now (2015-07-20) the latest stable version. Thus, this issue should be fixed by the next release.Scalise
L
8

Use plt.close() to fix this issue.

Lutenist answered 16/7, 2020 at 22:27 Comment(1)
That is the point. Simple and efficient. Thanks!Transcend
H
6

Sorry in advance for answering an old question but I came across a similar issue after installing Python 3.6.9 and matplotlib via pip on a machine running with a Linux distro. My intent was to be able to re-run old scripts which involved pyplot after upgrading Python on said machine. While the scripts ran until completion and provided the expected output, I was always getting this warning:

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

upon using the savefig() method from pyplot (I guess a similar problem would have occurred with the show() method as well). The line mentioned in the warning corresponds to this method found in 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)

I looked up on the matplotlib GitHub to check if there was a more recent version of the same script or if this problem was known, and it turns out the current implementation of the method above only rely on the self.close_event() instruction, i.e., the GLib.source_remove() is unnecessary.

Therefore, I commented the last 2 lines in the code above and saved the changes. After making this edit, I could run my scripts without getting any warning. I hope this will help people encountering a similar problem.

Hawger answered 27/3, 2020 at 15:38 Comment(3)
You're right, I had the same problem when using savefig from pyplot. However, the show method did not produce this error. Only the savefig method did. Not sure if a good solution for this bug has been implemented (instead of having to hack the source code of these libraries)?Ezraezri
I'm getting the same issue as @CuriousLeo with Python 3.8.2 and matplotlib 3.2.1.Pyrite
Note to future self: See answer by @user1959279 below to fix this issue with matplotlib3.2.1Pyrite

© 2022 - 2024 — McMap. All rights reserved.