-1

I'm working on an assignment which involves the need to show multiple figures on screen as the script is run (and also to save them). I am having to use both imshow and show to get the images up on the screen. This works fine, but I have read that show() should only be used once per script. Is there another way to display the image? The saved image files are also saving as blank 800x600 white images. Here's my code:

img = np.zeros((100,100))
plt.figure(0)
plt.imshow(img)
plt.show()
plt.savefig("images/img.png")
plt.close(0)

Each other figure is following the same syntax (obviously with different image names and a new figure number.

Thanks!

IceDragon
  • 299
  • 3
  • 7
  • 16
  • I have resolved the blank image issue by putting the savefig before the show command. Still unsure that this is the most efficient way to code what I am after? – IceDragon Mar 07 '13 at 10:54
  • Regarding efficiency: what do you mean? Is it important to you that a plot-once-in-a-lifetime code runs as fast as possible? Generally, plotting should happen within seconds and it should not be important if its one second or five. If you think you have an "efficiency" problem, then please show the actual code and describe your observations. – Dr. Jan-Philip Gehrcke Mar 07 '13 at 11:24
  • Still not clear if you have an actual problem left. – Dr. Jan-Philip Gehrcke Mar 07 '13 at 12:17
  • I have read that show() should only be used once, at the end, of a script. Is there any other way to have the images display in a figure multiple times? Or should I stick to 'if it ain't broke, don't fix it'? – IceDragon Mar 07 '13 at 12:24
  • Where is that written? If it works, then it's fine, generally, yes. If you want people to provide actual criticism with respect to your code, then show actual code. – Dr. Jan-Philip Gehrcke Mar 07 '13 at 12:26
  • I thought I had. I will just take it as it is for now, thanks. – IceDragon Mar 07 '13 at 12:40
  • 1
    To you it might not seem obvious, but a code example should be easily executable by others and reproduce the issue. If there is no specific issue, then at least include the parts that you might think are problematic. It's fine now. But you might realize that you came here without really knowing what your question is. – Dr. Jan-Philip Gehrcke Mar 07 '13 at 13:11

1 Answers1

1

Generally, your approach using figure() to create a new figure object for each figure you want to display on the screen and save to a file is fine, if this is what you want to hear.

I'm not sure what your actual question is in this respect, so I would seriously recommend editing your question if there is something else you want to know.

Regarding the second issue: depending on the backend in use, show() may destroy objects in the figure (upon closing), which is why you generally should first savefig() and then show(). This is documented here.

Dr. Jan-Philip Gehrcke
  • 33,287
  • 14
  • 85
  • 130