Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I close all pyplot windows (including ones from previous script executions)?

So I have some python code that plots a few graphs using pyplot. Every time I run the script new plot windows are created that I have to close manually. How do I close all open pyplot windows at the start of the script? Ie. closing windows that were opened during previous executions of the script?

In MatLab this can be done simply by using closeall.

like image 512
Ron Ronson Avatar asked Nov 22 '15 10:11

Ron Ronson


People also ask

What does PLT close () do?

plt. close() closes a window, which will be the current window, if not specified otherwise.

How to close a Python plot window?

plt.figure ().close (): Close a figure window. close (name), where name is a string, closes the figure with that label Now, swap the statements "plt.show ()" and "plt.close ()" in the code. You wouldn't get to see any plot as the output because the plot would already have been closed.

How do I clear the current state of a Pyplot plot?

How to Clear a Pyplot Figure. Figure is the top-level container object in a matplotlib plot. Figure includes everything visualized in a plot, including one or more Axes. You can use the matplotlib.pyplot.clf () function to clear the current Figure’s state.

How do I kill all instances of a window in Python?

On *nix you can use killall command. closes every instance of window with app for the window name. You can also use the same command from inside your python script. You can use os.system ("bashcommand") to run the bashcommand.

How do I clear two axes in a plot in Python?

Only the second Axes is cleared with the cla () function: Figure 3. A Figure containing two Axes in different subplots. The first Axes is not cleared with the cla () function. The second Axes is cleared with cla (): Use ActiveState Python and accelerate your Python Data Science projects.


3 Answers

To close all open figures from a script, you can call

plt.close('all')

or you can terminate the associated Python process.

like image 126
jakevdp Avatar answered Oct 17 '22 02:10

jakevdp


import matplotlib.pyplot as plt
plt.close("all")

(In case you have pyplot already imported, you obviously don't need to import it again. In that case just make sure to replace plt in plt.close("all") with whatever alias you chose for pyplot when you imported it.)

like image 5
Adrian Avatar answered Oct 17 '22 03:10

Adrian


This solution won't allow you to close plots from previous runs, but will prevent you from leaving them open!

The only way I have found to close these "hanging" figures is to find the process and kill.

Plot in a non blocking manner then ask for input. This should prevent you from forgetting to properly close the plot.

plt.show(block=False)
plt.pause(0.001) # Pause for interval seconds.
input("hit[enter] to end.")
plt.close('all') # all open plots are correctly closed after each run
like image 5
Ian Zurutuza Avatar answered Oct 17 '22 04:10

Ian Zurutuza