72

My question is simple: I have a python script that generates figures using matplotlib. Every time i run it it generates new windows with figures. How can I have the script close windows that were opened the previous time it ran?

the analogous command in matlab is to put 'close all' at the beginning of your matlab script.

I have seen several suggestions to do something like

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

This solution works if you run your script from the python shell, eg using

>>>> execfile("myScript.py")

However, I have found that this doesn't work if I run the script using Eclipse / PyDev. How can I get it to work in Eclipse?

example:

from numpy import *
from matplotlib.pyplot import *
from scipy import *

close("all") 
    #close any previously open plots - this doesn't work when running via Eclipse

t = linspace(0, 0.1,1000)
w = 60*2*pi

figure()
plot(t,cos(w*t))
plot(t,cos(w*t-2*pi/3))
plot(t,cos(w*t-4*pi/3))
show()

This should plot the ideal waveforms for a nice 3-phase power supply.

EBH
  • 10,350
  • 3
  • 34
  • 59
AnjoMan
  • 4,838
  • 4
  • 20
  • 28

4 Answers4

91

You can close a figure by calling matplotlib.pyplot.close, for example:

from numpy import *
import matplotlib.pyplot as plt
from scipy import *

t = linspace(0, 0.1,1000)
w = 60*2*pi


fig = plt.figure()
plt.plot(t,cos(w*t))
plt.plot(t,cos(w*t-2*pi/3))
plt.plot(t,cos(w*t-4*pi/3))
plt.show()
plt.close(fig)

You can also close all open figures by calling matplotlib.pyplot.close("all")

Bi Rico
  • 25,283
  • 3
  • 52
  • 75
  • 3
    This works from a console (e.g. running '>>> execfile("script.py")' in the python shell), but not in Eclipse/PyDev. What is the difference? – AnjoMan Feb 21 '13 at 22:50
  • Nice. Add a random vector to cos(w*t-4*pi/3) for noise. – Andrew Nov 09 '14 at 18:04
  • 9
    @AnjoMan why did you accept this answer? it clearly does not answer your question. – abcd May 28 '15 at 05:35
  • 6
    @dbliss I honestly can't remember. I think the way I solved this problem was "dont use eclipse". – AnjoMan May 28 '15 at 14:33
3

See Bi Rico's answer for the general Eclipse case.

For anybody - like me - who lands here because you have lots of windows and you're struggling to close them all, just killing python can be effective, depending on your circumstances. It probably works under almost any circumstances - including with Eclipse.

I just spawned 60 plots from emacs (I prefer that to eclipse) and then I thought my script had exited. Running close('all') in my ipython window did not work for me because the plots did not come from ipython, so I resorted to looking for running python processes.

When I killed the interpreter running the script, then all 60 plots were closed - e.g.,

$ ps aux | grep python
rsage    11665  0.1  0.6 649904 109692 ?       SNl  10:54   0:03 /usr/bin/python3 /usr/bin/update-manager --no-update --no-focus-on-map
rsage    12111  0.9  0.5 390956 88212 pts/30   Sl+  11:08   0:17 /usr/bin/python /usr/bin/ipython -pylab
rsage    12410 31.8  2.4 576640 406304 pts/33  Sl+  11:38   0:06 python3 ../plot_motor_data.py
rsage    12431  0.0  0.0   8860   648 pts/32   S+   11:38   0:00 grep python

$ kill 12410

Note that I did not kill my ipython/pylab, nor did I kill the update manager (killing the update manager is probably a bad idea)...

sage
  • 4,863
  • 2
  • 44
  • 47
  • +1 I had similar issues using PyCharm, the key point for me was killing the python process which is spawned by the PyCharm editor and guess in other Emacs/Eclipse/PyDev. In PyCharm this is just clicking on the red "X" in the bottom window. – shelbypereira Oct 16 '17 at 11:35
0

It will kill not only all plot windows, but all processes that are called python3, except the current script you run. It works for python3. So, if you are running any other python3 script it will be terminated. As I only run one script at once, it does the job for me.

import os
import subprocess
subprocess.call(["bash","-c",'pyIDs=($(pgrep python3));for x in "${pyIDs[@]}"; do if [ "$x" -ne '+str(os.getpid())+' ];then  kill -9 "$x"; fi done'])
Vitor Abella
  • 1,213
  • 8
  • 15
-1

Nothing works in my case using the scripts above but I was able to close these figures from eclipse console bar by clicking on Terminate ALL (two red nested squares icon).

Ibraheem
  • 35
  • 8
  • Read question carefully. Author is asking for a way to close from the script. > How can I have **the script close** windows that were opened the previous time it ran? – Liastre Oct 29 '19 at 11:11
  • You can not. I think the person asked the question needs to close these windows without closing eclipse. – Ibraheem Oct 30 '19 at 13:50