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
#
# ######################################################################################