How to Close All Figures in Python?

Estimated read time 1 min read

In Matplotlib, you can close all open figures by calling the close function with no arguments. This will close all currently open figures:

import matplotlib.pyplot as plt

# ... code for creating figures ...

# Close all open figures
plt.close()

Alternatively, if you want to close a specific figure, you can pass a reference to it as an argument to the close function:

import matplotlib.pyplot as plt

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

# ... code for manipulating figures ...

# Close specific figure
plt.close(fig1)

Note that closing a figure will also release any associated memory and resources used by that figure, so it’s a good practice to close figures that are no longer needed to free up resources.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply