从python脚本自动关闭输出窗口

时间:2021-12-01 17:04:21

I have written a little script to batch plot some data. This script cannot run without a window displaying the newly created plot opening. It stops running till the window is manually closed, then the script continues to run. So my question is as follows:

我写了一个小脚本来批量绘制一些数据。如果没有显示新创建的绘图空间的窗口,则此脚本无法运行。它会停止运行,直到手动关闭窗口,然后脚本继续运行。所以我的问题如下:

Is there a way automatically close an output window from within a python script?

有没有办法从python脚本中自动关闭输出窗口?

Here is the script

这是脚本

import pynbody
import numpy as np
import pynbody.plot.sph as sph

f = open('files.txt', 'r')
for line in f:
     line = line[0:-1]
     s = pynbody.load(line)
     sph.image(s.gas,qty="temp",width=16, filename=line[0:-6]+line[-5:len(line)],  units='K',  cmap="YlOrRd",  ret_im=False , approximate_fast=False)

#At the end of the loop I would like to close the output window so the script 
#will continue to run 

The pynbody.plot.sph package seems as it should be able to turn the output window on and off within the sph.image function call but I have exhausted all the possibilities and am now resorting to this to get the script running.

pynbody.plot.sph包似乎应该能够在sph.image函数调用中打开和关闭输出窗口但是我已经用尽了所有的可能性,现在我正在使用它来运行脚本。

3 个解决方案

#1


0  

According to the documentation, you should set the noplot argument to True

根据文档,您应该将noplot参数设置为True

#2


0  

Add a line pynbody.plot.plt.close() to the for loop.

在for循环中添加一行pynbody.plot.plt.close()。

Definition: pynbody.plot.plt.close(*args) Docstring: Close a figure window.

定义:pynbody.plot.plt.close(* args)Docstring:关闭一个数字窗口。

close() by itself closes the current figure

close()本身会关闭当前的数字

close(h) where h is a :class:Figure instance, closes that figure

close(h)其中h是a:class:Figure instance,关闭该数字

close(num) closes figure number num

close(num)关闭图号num

close(name) where name is a string, closes figure with that label

close(name)其中name是一个字符串,用该标签关闭数字

close('all') closes all the figure windows

close('all')关闭所有数字窗口

#3


0  

I found the answer using this link

我找到了这个链接的答案

view and then close the figure automatically in matplotlib?

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

Essentially, here's the answer:

基本上,这是答案:

plt.show(block=False) # block = False allows automatic closing of the image
# the following command will close the graph in 3 seconds
time.sleep(3)
plt.close()

#1


0  

According to the documentation, you should set the noplot argument to True

根据文档,您应该将noplot参数设置为True

#2


0  

Add a line pynbody.plot.plt.close() to the for loop.

在for循环中添加一行pynbody.plot.plt.close()。

Definition: pynbody.plot.plt.close(*args) Docstring: Close a figure window.

定义:pynbody.plot.plt.close(* args)Docstring:关闭一个数字窗口。

close() by itself closes the current figure

close()本身会关闭当前的数字

close(h) where h is a :class:Figure instance, closes that figure

close(h)其中h是a:class:Figure instance,关闭该数字

close(num) closes figure number num

close(num)关闭图号num

close(name) where name is a string, closes figure with that label

close(name)其中name是一个字符串,用该标签关闭数字

close('all') closes all the figure windows

close('all')关闭所有数字窗口

#3


0  

I found the answer using this link

我找到了这个链接的答案

view and then close the figure automatically in matplotlib?

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

Essentially, here's the answer:

基本上,这是答案:

plt.show(block=False) # block = False allows automatic closing of the image
# the following command will close the graph in 3 seconds
time.sleep(3)
plt.close()