5

Python puts out a memory error after drawing a few graphs with pyplot (I draw plots with over a million points on a laptop - and how many graphs can be drawn before the error has direct relationship with the amount of points).

How can the memory be cleared after, so that I can draw more graphs? The only option now is to ctrl + . to restart the kernel.

I have tried the recommended:

matplotlib.pyplot.close("all")
matplotlib.pyplot.clf()

Doesn't clear the errors.

Alireza
  • 100,211
  • 27
  • 269
  • 172
Tony
  • 343
  • 1
  • 6
  • 15

3 Answers3

2

I've been battling this for weeks and the only thing that worked for me was the solution presented here:

How to clear memory completely of all Matplotlib plots

matplotlib.pyplot.figure().clear()
matplotlib.pyplot.close()

The following:

plt.cla()

and

plt.clf() 

didn't work for me at all... I suspect because it was designed for when you have more than one subplot...

Adrian
  • 64
  • 5
1

Assuming you are working with Jupyter Notebook you can just invoke %reset in a cell.

petezurich
  • 9,280
  • 9
  • 43
  • 57
-1

Maybe this has been fixed in more recent revisions, but I still use this:

plt.close()
# http://matplotlib.org/users/pyplot_tutorial.html
# >>> WORKING WITH MULTIPLE FIGURES AND AXES
# is a must

The pyplot tutorial explains:

If you are making lots of figures, you need to be aware of one more thing: the memory required for a figure is not completely released until the figure is explicitly closed with .close().

Deleting all references to the figure, and/or using the window manager to kill the window in which the figure appears on the screen, is not enough, because pyplot maintains internal references until .close() is called.


If not handled, can also crash the python interpreter as a whole

Had a lot of headache with this.

# #####################################################################################
#
# AFTER SOME 50 calls, matplotlib crashed first, after a few more, python was killed too
#
# ###################################################################################### >>> http://stackoverflow.com/questions/32318643/how-to-properly-delete-a-matplotlib-figure-embedded-in-a-child-window-in-pyside
#
# Traceback (most recent call last):
#   File "C:\Python27.anaconda\lib\site-packages\matplotlib\backends\backend_qt5agg.py", line 91, in paintEvent
#     stringBuffer = self.renderer._renderer.tostring_bgra()
# MemoryError
# Traceback (most recent call last):
#   File "C:\Python27.anaconda\lib\site-packages\matplotlib\backends\backend_qt5agg.py", line 91, in paintEvent
#     stringBuffer = self.renderer._renderer.tostring_bgra()
# MemoryError
# Traceback (most recent call last):
#   File "C:\Python27.anaconda\lib\site-packages\matplotlib\backends\backend_qt5agg.py", line 91, in paintEvent
#     stringBuffer = self.renderer._renderer.tostring_bgra()
# MemoryError
# Traceback (most recent call last):
#   File "C:\Python27.anaconda\lib\site-packages\matplotlib\backends\backend_qt5agg.py", line 91, in paintEvent
#     stringBuffer = self.renderer._renderer.tostring_bgra()
# MemoryError
#
# ######################################################################################
ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
user3666197
  • 1
  • 6
  • 50
  • 92
  • The questioner already has tried using `matplotlib.pyplot.close("all")`. In how far does your answer provide anything new to solve this problem? – ImportanceOfBeingErnest Jul 02 '17 at 08:22
  • I'm trying to make this answer useful for everyone. There is no agression involved here. I did also not delete any content, but made it according to the formatting guidelines. Citations with `>` would require to make it clear where the citation comes from (linking to other content) otherwise it should be plain text. Headlines are meant to be used as titles for sections, not to make normal content bigger. It's still not clear to me, what this answer adds to help the questioner, so you may want to explain better, in how far you think that `plt.close()` is different from `plt.close("all")`. – ImportanceOfBeingErnest Jul 02 '17 at 12:28
  • user3666197: I have reported your remark to a moderator. We have spoken about this a great deal: please be willing to accept reasonable revisions to your work. The quote issue that @Importance mentions comes up a lot. If material comes from elsewhere and you cite a source, then it's a quote. Quote blocks are not general highlighter devices. – halfer Jul 02 '17 at 14:21
  • 1
    @halfer The paragraph in question actually *is* a quote. It took me a while to find out that it is part of the pyplot tutorial page. I have edited the answer to make its origin clear. My doubts about the usefulness of this answer still remains. – ImportanceOfBeingErnest Jul 02 '17 at 14:55
  • @Importance: good sleuthing. Nevertheless, quote blocks where the source is unclear are of dubious value, IMO. Unfortunately the quote device is so frequently misused as a general highlighter on this site, that I will usually unquote them, assuming the same has happened again. In any case, readers or editors should not have to go fishing for this sort of thing. – halfer Jul 02 '17 at 15:00