从Eclipse运行时,关闭matplotlib中已有的图形 [英] Close pre-existing figures in matplotlib when running from eclipse

查看:165
本文介绍了从Eclipse运行时,关闭matplotlib中已有的图形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题很简单:我有一个使用matplotlib生成图形的python脚本.每次我运行它时,都会生成带有数字的新窗口.如何让脚本关闭上次运行时打开的窗口?

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?

matlab中的类似命令是将全部关闭"放在matlab脚本的开头.

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")

如果您从python shell运行脚本,例如使用

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

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

但是,我发现如果我使用Eclipse/PyDev运行脚本,这将不起作用.如何在Eclipse中使用它?

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?

示例:

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.

推荐答案

您可以通过调用matplotlib.pyplot.close来关闭图形,例如:

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)

您也可以通过调用matplotlib.pyplot.close("all")

这篇关于从Eclipse运行时,关闭matplotlib中已有的图形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆