2

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.

7
  • What OS are you running? Commented May 12, 2014 at 16:37
  • linux (Mageia KDE 4.11) Commented May 12, 2014 at 16:39
  • Ok, well I can't help you on the specifics, but on windows, the window should show up as a separate process. If you can find the PID you can kill it with os.kill() Commented May 12, 2014 at 16:50
  • I need to kill about 100 windows :P Commented May 12, 2014 at 17:00
  • Right, but if your script can find the PIDs, its no extra work for you Commented May 12, 2014 at 17:20

3 Answers 3

1

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()
Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

The little blurb at the end of the post was to clarify that I have exhausted all the options that the function gives me. I have read the documentation and tried setting noplot=True as well as the ret_im flag.
0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.