Close pre-existing figures in matplotlib when running from eclipse Close pre-existing figures in matplotlib when running from eclipse python python

Close pre-existing figures in matplotlib when running from eclipse


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

from numpy import *import matplotlib.pyplot as pltfrom scipy import *t = linspace(0, 0.1,1000)w = 60*2*pifig = 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")


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 pythonrsage    11665  0.1  0.6 649904 109692 ?       SNl  10:54   0:03 /usr/bin/python3 /usr/bin/update-manager --no-update --no-focus-on-maprsage    12111  0.9  0.5 390956 88212 pts/30   Sl+  11:08   0:17 /usr/bin/python /usr/bin/ipython -pylabrsage    12410 31.8  2.4 576640 406304 pts/33  Sl+  11:38   0:06 python3 ../plot_motor_data.pyrsage    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)...


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 osimport subprocesssubprocess.call(["bash","-c",'pyIDs=($(pgrep python3));for x in "${pyIDs[@]}"; do if [ "$x" -ne '+str(os.getpid())+' ];then  kill -9 "$x"; fi done'])