Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clearing a specific figure in MatPlotLib

In MatPlotLib, I can use:

plt.gcf().clear()

To clear the contents of the current figure.

But suppose I have two figures, created with:

fig1 = plt.figure(1)
fig2 = plt.figure(2)

How can I clear a specific figure based on its number?

like image 436
Karnivaurus Avatar asked Apr 09 '26 00:04

Karnivaurus


1 Answers

.clear() is a method of the figure class. It doesn't matter if you obtain an instance of matplotlib.figure.Figure via plt.gcf() or via the variable you store it in. Therefore

fig1.clear()

will clear the figure stored in fig1.

Of course you can also directly use the call to plt.figure() to clear that figure,

plt.figure(1).clear()
like image 54
ImportanceOfBeingErnest Avatar answered Apr 11 '26 14:04

ImportanceOfBeingErnest



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!