Python 查看然后在 matplotlib 中自动关闭图形?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40395659/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 23:30:15  来源:igfitidea点击:

view and then close the figure automatically in matplotlib?

pythonmatplotlib

提问by zmwang

I have to check whether my parameters setting is right, so I need to draw many plots, and for drawing these plots, I choose to use matplotlib. After each checking, I need to click the close button on the top left conner. It's trivial. So is there any method that can make the plot show in about 3 or 5 seconds and automatically close without clicking? I know about the plt.close(), but it doesn't work. Here is my code.

我要检查我的参数设置是否正确,所以我需要绘制很多图,为了绘制这些图,我选择使用matplotlib。每次检查后,我需要单击左上角的关闭按钮。这是微不足道的。那么有没有什么方法可以让绘图在大约 3 或 5 秒内显示并自动关闭而不单击?我知道plt.close(),但它不起作用。这是我的代码。

from math import *
import sys
import numpy as np
from scipy import special 
import matplotlib.pyplot as plt

x1=[]
y1=[]
x2=[]
y2=[]
x3=[]
y3=[]
with open('fort.222','r') as f:
    for line in f:
        a,b=line.split()
        x1.append(float(a))
        y1.append(float(b))

n=int(sys.argv[1])
i=0
with open('fort.777','r') as f:
    for line in f:
        if i==0:
            k1=float(line)
        i=i+1

x1,y1=np.array(x1),np.array(y1)

z1=special.eval_hermite(n,x1*k1)*sqrt(1/(sqrt(pi)*pow(2,n)*factorial(n)))*sqrt(k1)*np.exp(-np.power(k1*x1,2)/2.)                                                                                                  
plt.figure()
plt.plot(x1,z1)
plt.plot(x1,y1)
plt.plot(x1,np.zeros(len(x1)))
plt.title('single center & double center')
plt.xlim(x1.min(),x1.max())
plt.ylim(-max(abs(y1.min()-0.1),y1.max()+0.1),max(abs(y1.min()-0.2),y1.max()+0.2))
plt.xlabel('$\zeta$'+'/fm')
plt.legend(('single, n='+sys.argv[1],'double, n='+sys.argv[1]),loc=2,frameon=True)

plt.show()
plt.close()

回答by Leon

Documentation on pyplot.show()reads:

关于pyplot.show()阅读的文件

matplotlib.pyplot.show(*args, **kw)

Display a figure. When running in ipython with its pylab mode, display all figures and return to the ipython prompt.

In non-interactive mode, display all figures and block until the figures have been closed; in interactive mode it has no effect unless figures were created prior to a change from non-interactive to interactive mode (not recommended). In that case it displays the figures but does not block.

A single experimental keyword argument, block, may be set to Trueor Falseto override the blocking behavior described above.

matplotlib.pyplot.show(*args, **kw)

显示一个图形。当以 pylab 模式在 ipython 中运行时,显示所有图形并返回 ipython 提示符。

在非交互模式下,显示所有图形并阻止直到图形关闭;在交互模式下,除非在从非交互模式更改为交互模式(不推荐)之前创建图形,否则它无效。在这种情况下,它会显示数字但不会阻止。

单个实验性关键字参数block可以设置为TrueFalse覆盖上述阻塞行为

So the solution is this:

所以解决方案是这样的:

plt.show(block=False)
plt.pause(3)
plt.close()