Matplotlib Figures not cleaned up during loop
See original GitHub issueIt seems like while generating plots in a loop, the memory grows over time when running the script in spyder.
I have a funny use-case where I’m making a lot of large plots, and then when I’m done I like to interactively look at the last plot that was generated.
To reproduce using this little script:
import sys
import gc
import numpy as np
import matplotlib
matplotlib.use('Qt4Agg')
import matplotlib.pyplot as plt
arr = np.random.rand(10000, 10000)
def memplot_plot(arr):
plt.matshow(arr)
plt.close()
print '*' * 80
for i in range(1, 10):
print 'i : ' + str(i)
print len( gc.get_objects())
print sys.getsizeof(gc.get_objects())
memplot_plot(arr)
gc.collect()
print '*' * 80
From the command-line
*****************************************
i : 1
61424
514568
i : 2
61710
514568
i : 3
61713
514568
i : 4
61716
514568
i : 5
61719
514568
i : 6
61722
514568
i : 7
61725
514568
i : 8
61728
514568
i : 9
61731
514568
*****************************************
From spyder, running in a dedicated terminal with interaction afterwards
********************************************************************************
i : 1
64416
578936
i : 2
67685
578936
i : 3
70737
578936
i : 4
73789
651352
i : 5
76841
651352
i : 6
79893
651352
i : 7
82945
732816
i : 8
85997
732816
i : 9
89049
732816
********************************************************************************
And for fun, my windows task manager. The four small blips are four runs from the commandline (python, python -i, ipython and ipython -i). The last one is from Spyder. And this is not clean because I’m also working in the background.
matplotlib.get_backend()
Out[2]: u'Qt4Agg'
matplotlib.__version__
Out[3]: '1.4.3'
Let me know if you need any more information. Same behavior was observed on a colleague’s machine.
Issue Analytics
- State:
- Created 8 years ago
- Comments:8 (6 by maintainers)
Top Results From Across the Web
Matplotlib runs out of memory when plotting in a loop
From @geographica's answer it seems that clf() clears the figure, but doesn't release all references to it, unlike close(fig) . It won't hold ......
Read more >How To Clear A Plot In Python (without closing the window)
Click to clear a plot using two methods - clearing Pyplot Figure or clearning PyPlot axes, without closing the plot window.
Read more >Interactive figures and asynchronous programming - Matplotlib
Once the event loop is stopped (leaving any still open figure windows non-responsive) you will be able to use the prompt again. Re-starting...
Read more >How to update a plot on same figure during the loop?
Here, figure.canvas.flush_events() is used to clear the old figure before plotting the updated figure. Real time plotting with Matplotlib.
Read more >How to clear the memory completely of all Matplotlib plots?
Using the following methods, we can clear the memory occupied by Matplotlib plots. plt.figure() - Create a new figure or activate an ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
So, should we close?
This is very old, let’s close it.