在从eclipse运行时,在matplotlib中关闭已有的数据

时间:2023-01-13 22:10:45

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?

我的问题很简单:我有一个python脚本,它使用matplotlib生成数据。每次我运行它,它都会生成带有图形的新窗口。我怎么能让脚本关闭前一次运行的窗口呢?

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中运行脚本(例如使用python),这个解决方案就可以工作

>>>> 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.

这将为一个不错的三相电源绘制理想的波形图。

2 个解决方案

#1


60  

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

您可以通过调用matplotli .pyplot来关闭一个图形。接近,例如:

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”)来关闭所有打开的图形。

#2


3  

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相比,我更喜欢这个),然后我认为我的脚本已经退出了。在我的ipython窗口中近距离运行('all')对我来说不起作用,因为这些图不是来自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,也没有杀死更新管理器(杀死更新管理器可能是个坏主意)……

#1


60  

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

您可以通过调用matplotli .pyplot来关闭一个图形。接近,例如:

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”)来关闭所有打开的图形。

#2


3  

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相比,我更喜欢这个),然后我认为我的脚本已经退出了。在我的ipython窗口中近距离运行('all')对我来说不起作用,因为这些图不是来自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,也没有杀死更新管理器(杀死更新管理器可能是个坏主意)……