33

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.

Ron Ronson
  • 445
  • 1
  • 4
  • 7
  • Related: [Closing pyplot windows](https://stackoverflow.com/q/11140787/4975230) – jrh Jan 30 '19 at 21:47

7 Answers7

39

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

plt.close('all')

or you can terminate the associated Python process.

jakevdp
  • 77,104
  • 11
  • 125
  • 160
  • 16
    That doesn't close figures that were open from previous executions of the script which is what I need. – Ron Ronson Nov 22 '15 at 14:28
  • 1
    It will close all figures in the python process that runs the command. It sounds like you have multiple Python processes running: in that case you need to either run this command in the associated process, or kill the desired process. – jakevdp Nov 22 '15 at 14:52
8

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
Ian Zurutuza
  • 588
  • 5
  • 16
  • this is the only solution that worked for me. however, it has the drawback that the figure(s) pop(s) up for a short time before being closed, which is typically what you'd like to avoid in this scenario. Any workaround? – user9998992 Dec 06 '22 at 12:08
5
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.)

Adrian
  • 551
  • 5
  • 16
3

I just had the same problem. I call a function that generates multiple plot windows. Each time I call the function the popped up plot windows accumulate in number. Trying matplotlib.pyplot.close('All') at the begining of the function didn't solve the problem. I solved the problem by calling matplotlib.pyplot.close(figure), where figure is a plot figure instance (object). I maintain a list of my plot figure objects. So, it is a good idea to maintain a list, and then call matplotlib.pyplot.close(figure) for each instance of a figure object:

import matplotlib.pyplot as plot

Add plot instance objects (figure,axis) to a list:

fig, (ax1,ax2) = plt.subplots(nrows=2)
figAxisDict = {'figure': fig, 'axis1': ax1, 'axis2':ax2}

figAxisList.append(figAxisDict)

Call the function for figure closing, and clear the list afterwards:

if len(figAxisList) !=0:

    for figAxis in figAxisList:
        figure=figAxis['figure']

        plot.close(figure)

    figAxisList[:]=[] 
Marjan100
  • 346
  • 1
  • 2
  • 16
0

As there seems no absolutely trivial solution to do this automatically from the script itself: the possibly simplest way to close all existing figures in pycharm is killing the corresponding processes (as jakevdp suggested in his comment):

Menu Run\Stop... (Ctrl-F2). You'll find the windows closed with a delay of few seconds.

FlorianH
  • 600
  • 7
  • 18
0

Honestly, the developer needs to make this a simple function like it is in MATLAB.

My temporary solution for Spyder:

  1. first install keyboard and pynput via the Anaconda Prompt
  2. in Spyder run "mouse.position" with your mouse over the position where your plots pane is

These coordinates are the ones you need to replace in the code below.

import keyboard
from pynput.mouse import Button, Controller

#%% Clear Old Plots
#Selects the mouse as the controller
mouse = Controller()

#Record current mouse position
recmp = mouse.position

#Set pointer position on plots pane
mouse.position = (1136,370) # <-- replace coordinates here with yours!

#Opens the plot pane
keyboard.press_and_release('ctrl+shift+g')

#Click and release left mouse button
mouse.press(Button.left)
mouse.release(Button.left)

#Runs close all in the plots pane
keyboard.press_and_release('ctrl+shift+w')

#Resets mouse position to the original location
mouse.position = recmp
Just Mike
  • 1
  • 1
-5

On *nix you can use killall command.

killall app

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.

Bibek_G
  • 199
  • 1
  • 2
  • 12