Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

7
  • Bah! I'd forgotten the clf() call... Adding that fixed it. Commented Mar 3, 2010 at 12:44
  • 10
    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 much memory when cleared, but if there's many, it'll add up... Commented Jul 2, 2013 at 23:48
  • 1
    So if I have a real time plot of about 30k values and I have other arcs,ellipses.circles that I need to plot, what will pyplot.clf() do exactly? Also for animation I have used pyplot.pause(time_duration). How do I preserve the values I have plotted before and then plot the values again and again as I get the data? Commented Apr 11, 2014 at 14:33
  • 2
    For me it also helped to manually run Python's garbage collection from time to time to get rid of out of memory errors. For this I added import gc at the top of my code and then gc.collect() after close() (or every now and then, depending on the code). Commented Mar 7, 2019 at 10:38
  • 6
    I was able to fix my problem with both fig.clf() AND plt.close(fig). Otherwise, by only using plt.close(fig), I still OOMed with more than 10 Gb when plotting and saving ~8000 images in Kaggle notebook. Commented Jun 28, 2020 at 21:54