-1

I have a python program, say, train.py. It can be run in anaconda prompt by typing:

python train.py

In train.py, some part is written for drawing and saving figures:

import matplotlib.pyplot as plt
....... #I omit these codes
plt.savefig(....)
plt.show() #this will produce a figure window 
plt.close()

In implementing the program, some figures are to be generated, which will bring the program to a temporary halt, even though plt.close() present. Then I need to manually close the pop-up figure window due to plt.show() and continue the program. How to avoid this inconvenience.

It is noted that spyder can run the program continuously, with figures displayed in its console.

jwm
  • 4,832
  • 10
  • 46
  • 78
  • Is there a reason you would use `plt.show()` to be immediately followed by an attempt to close that window? – roganjosh Jul 14 '17 at 20:23
  • I still want to see these figures, and then they are closed by coding – jwm Jul 14 '17 at 20:24
  • Have you read the answer [here](https://stackoverflow.com/questions/11140787/closing-pyplot-windows)? Did it help? – roganjosh Jul 14 '17 at 20:27
  • Do you want the figures to be displayed for a certain time period, say 10 seconds, and then the windows be closed? – Rory Daulton Jul 14 '17 at 20:36

1 Answers1

1

plt.show() is meant to be used once in a script to show all figures present. So you can create your figures and show them all at the end

fig1 = plt.figure(1)
# do something with figure 1

fig2 = plt.figure(2)
# do something with figure 2

plt.show() # show both figures
ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712