質問

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?

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.

役に立ちましたか?

解決

I found the answer using this link

view and then close the figure automatically in 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()

他のヒント

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

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

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

close() by itself closes the current figure

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

close(num) closes figure number num

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

close('all') closes all the figure windows

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top