Python 从 Eclipse 运行时关闭 matplotlib 中预先存在的图形
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15012309/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Close pre-existing figures in matplotlib when running from eclipse
提问by AnjoMan
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?
我的问题很简单:我有一个使用 matplotlib 生成图形的 python 脚本。每次我运行它时,它都会生成带有数字的新窗口。如何让脚本关闭上次运行时打开的窗口?
the analogous command in matlab is to put 'close all' at the beginning of your matlab script.
matlab 中的类似命令是将“全部关闭”放在 matlab 脚本的开头。
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
如果您从 python shell 运行脚本,则此解决方案有效,例如使用
>>>> 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?
但是,我发现如果我使用 Eclipse/PyDev 运行脚本,这将不起作用。我怎样才能让它在 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.
这应该为一个漂亮的三相电源绘制理想的波形。
回答by Bi Rico
You can close a figure by calling matplotlib.pyplot.close, for example:
您可以通过调用来关闭图窗matplotlib.pyplot.close,例如:
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")
您还可以通过调用关闭所有打开的数字 matplotlib.pyplot.close("all")
回答by sage
See Bi Rico's answer for the general Eclipse case.
请参阅 Bi Rico 对一般 Eclipse 案例的回答。
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.
对于任何人 - 像我一样 - 因为你有很多窗户而且你正在努力关闭它们而来到这里的任何人,根据你的情况,杀死 python 可能是有效的。它几乎可以在任何情况下工作——包括 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.
我刚刚从 emacs 中生成了 60 个图(我更喜欢那个而不是 eclipse),然后我以为我的脚本已经退出了。close('all')在我的 ipython 窗口中运行对我不起作用,因为这些图不是来自 ipython,所以我求助于寻找正在运行的 python 进程。
When I killed the interpreter running the script, then all 60 plots were closed - e.g.,
当我杀死运行脚本的解释器时,所有 60 个图都关闭了 - 例如,
$ 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)...
请注意,我没有杀死我的 ipython/pylab,也没有杀死更新管理器(杀死更新管理器可能是个坏主意)...
回答by Vitor Abella
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.
它不仅会杀死所有绘图窗口,还会杀死所有称为 python3 的进程,除了您运行的当前脚本。它适用于python3。因此,如果您正在运行任何其他 python3 脚本,它将被终止。由于我一次只运行一个脚本,它为我完成了这项工作。
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'])
回答by Ibraheem
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).
使用上面的脚本在我的情况下没有任何效果,但我能够通过单击Terminate ALL(两个红色嵌套方块图标)从 Eclipse 控制台栏中关闭这些数字。

![Python:正则表达式 findall 返回一个列表,为什么尝试访问列表元素 [0] 会返回错误?](/res/img/loading.gif)