Python 如何关闭所有 pyplot 窗口(包括以前脚本执行中的窗口)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33853801/
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
How do I close all pyplot windows (including ones from previous script executions)?
提问by Ron Ronson
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?
所以我有一些使用 pyplot 绘制一些图形的 python 代码。每次我运行脚本时都会创建新的绘图窗口,我必须手动关闭这些窗口。如何在脚本开始时关闭所有打开的 pyplot 窗口?IE。关闭在先前执行脚本期间打开的窗口?
In MatLab this can be done simply by using closeall
.
在 MatLab 中,这可以通过使用closeall
.
回答by Bibek_G
On *nix you can use killall
command.
在 *nix 上,您可以使用killall
命令。
killall app
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.
使用窗口名称的 app 关闭窗口的每个实例。您还可以在 Python 脚本中使用相同的命令。
您可以使用os.system("bashcommand")
bash 命令来运行。
回答by jakevdp
To close all open figures from a script, you can call
要从脚本中关闭所有打开的图形,您可以调用
plt.close('all')
or you can terminate the associated Python process.
或者您可以终止关联的 Python 进程。
回答by Adrian
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.)
(如果您已经导入了 pyplot,您显然不需要再次导入它。在这种情况下,只需确保plt
在plt.close("all")
导入时为 pyplot 选择的任何别名替换。)
回答by FlorianH
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):
由于似乎没有绝对微不足道的解决方案可以从脚本本身自动执行此操作:关闭 pycharm 中所有现有图形的可能最简单的方法是终止相应的进程(如 jakevdp 在他的评论中建议的那样):
Menu Run\Stop... (Ctrl-F2). You'll find the windows closed with a delay of few seconds.
菜单运行\停止... (Ctrl-F2)。您会发现窗户会延迟几秒钟关闭。
回答by Ian Zurutuza
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
回答by user3597222
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:
我只是遇到了同样的问题。我调用了一个生成多个绘图窗口的函数。每次我调用该函数时,弹出的绘图窗口都会累积数量。matplotlib.pyplot.close('All')
在函数开始时尝试并没有解决问题。我通过调用解决了这个问题matplotlib.pyplot.close(figure)
,其中 figure 是一个绘图图实例(对象)。我维护我的绘图图形对象的列表。因此,最好维护一个列表,然后调用matplotlib.pyplot.close(figure)
图形对象的每个实例:
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[:]=[]