The difference between plt.close () and plt.clf ()

In Python, what is the difference between plt.clf() and plt.close() ?

Will they work the same way?

+8
python matplotlib
source share
3 answers

plt.close() completely closes the picture window, where plt.clf() just clears the figure - you can still draw another plot on it.

It seems that for your needs you should give preference to plt.clf() or it is better to save the descriptor of the line objects themselves (they are returned in the lists with calls to plot ) and use .set_data for those subsequent iterations.

+15
source share

I think it's worth mentioning that plt.close() frees up memory, so it is preferable when creating and saving many shapes in one pass.

Using plt.clf() , in this case a warning is issued after 20 charts (even if they will not be displayed plt.show() ):

Over 20 digits have been discovered. Figures created through the pyplot interface ( matplotlib.pyplot.figure ) are kept until explicitly closed and may consume too much memory.

+2
source share

plt.clf() clears the entire current digit with all its axes, but leaves the open window such that it can be reused for other graphs.

plt.close() closes the window, which will be the current window, unless otherwise specified.

+1
source share

All Articles