How to shut down all open GUI in matplotlib
When working with Matplotlib, a common challenge is managing multiple open graphical user interface (GUI) windows that can accumulate during script execution. To efficiently handle this issue, one can use the plt.close()
function. This method allows for the closing of all active figures. For instance, using plt.close('all')
at the start of your script ensures that any previously opened figures are closed before new ones are displayed. This approach not only keeps your workspace tidy but also prevents potential confusion caused by lingering figures.
Matplotlib provides several methods to close open GUI windows, each with its own significance and application. Below, we explore the most effective methods to shut down all open GUI in Matplotlib.
1. Using plt.close('all')
to Close All Matplotlib Windows
The simplest and most direct way to close all open figures is by using the command:
import matplotlib.pyplot as plt
plt.close('all')
Here’s a straightforward example demonstrating the use of plt.close('all')
:
import matplotlib.pyplot as plt
fig1 = plt.figure()
fig2 = plt.figure()
plt.plot([1, 2, 3], label='Figure 1')
plt.legend()
plt.figure(fig2.number)
plt.plot([3, 2, 1], label='Figure 2')
plt.legend()
# Close all figures at once
plt.close('all')
This method is significant because it effectively clears all existing figure windows, regardless of how many were created during the session. It ensures that no residual figures remain, thus preventing clutter and confusion in the visual output.
Another approach involves with a timed closure:
The time.sleep(10)
command keeps the window open for 10 seconds, and after this time, the plt.close()
function is called to close the plot window automatically. This approach ensures the plot is visible for a set duration before closing without user interaction
import matplotlib.pyplot as plt
import time
plt.figure()
plt.plot([1, 2, 3])
plt.show(block=False)
time.sleep(10) # Keep the figure open for 1o seconds
plt.close() # Close the figure after 10 seconds
Method 2: Closing Specific Matplotlib Figures
You can also close specific figures by referencing their identifiers:
fig1= plt.figure()
ig2 = plt.figure()
plt.close(fig1)
This method allows for targeted closure of specific figures while leaving others open. It is significant because it provides flexibility in managing multiple plots when only certain visualizations are no longer needed. This is particularly helpful during iterative development where you may want to keep some figures visible for comparison.
import matplotlib.pyplot as plt
fig1 = plt.figure()
plt.plot([1, 2, 3], [4, 5, 6])
plt.title("Figure 1")
fig2 = plt.figure()
plt.plot([1, 2, 3], [6, 5, 4])
plt.title("Figure 2")
plt.close(fig1) # Close the first figure
plt.show() # Show remaining figures
Output:

Method 3: Closing Matplotlib Windows with Keyboard Events
You can enhance user interaction by binding keyboard events to close figures:
import matplotlib.pyplot as plt
def close_figure(event):
if event.key == 'q':
plt.close(event.canvas.figure)
fig = plt.figure()
fig.canvas.mpl_connect('key_press_event', close_figure)
plt.plot([1, 2, 3])
plt.show()
This method is significant because it allows users to close figures dynamically using keyboard shortcuts, improving workflow efficiency. This interactivity can be particularly useful during presentations or live coding sessions where quick figure management is essential.