在matplotlib中自动查看并关闭图形?

2024-04-24 22:12:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我必须检查我的参数设置是否正确,因此我需要绘制许多绘图,对于绘制这些绘图,我选择使用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()

Tags: importplotasnpsyslinepltk1
1条回答
网友
1楼 · 发布于 2024-04-24 22:12:40

Documentation on ^{} reads

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 True or False to override the blocking behavior described above.

所以解决办法是:

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

相关问题 更多 >