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

alvas
  • 115,346
  • 109
  • 446
  • 738
Martin Thoma
  • 124,992
  • 159
  • 614
  • 958

3 Answers3

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.

elya5
  • 2,236
  • 1
  • 11
  • 27
  • Note: the commit that fixed this issue came a few days *after* the release of v1.4.3 (released on 2015-02-16 https://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. – Gabriel Jul 20 '15 at 15:57
7

Use plt.close() to fix this issue.

jizhihaoSAMA
  • 12,336
  • 9
  • 27
  • 49
DrYJ
  • 93
  • 3
  • 6
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.

Jef Grailet
  • 547
  • 1
  • 5
  • 7
  • 2
    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)? – Curious Leo May 21 '20 at 23:41
  • I'm getting the same issue as @CuriousLeo with Python 3.8.2 and matplotlib 3.2.1. – David_G Jul 13 '20 at 01:52
  • 1
    Note to future self: See answer by @user1959279 below to fix this issue with matplotlib3.2.1 – David_G Jul 29 '20 at 01:15