11

I am trying to open a series of .png plots. I want to be able to view a plot on the screen and then get a prompt waiting for me to 'press enter'. On hitting enter, the next plot should be shown. I have seen many questions similar to this (Matplotlib - Force plot display and then return to main code) but when I do this I then have to manually click X on the top right-hand-side of the plot window to close it and only then does the code continue.

I am using python 2.7.8

Here is my code:

from PIL import Image
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import string
import sys
import shutil

fig=plt.figure()

Viewingfile = sys.argv[1]


for test_file in open(Viewingfile, "r").readlines(): 

    fig.set_tight_layout(True)
    plt.ion()
    image=mpimg.imread(test_file + ".ps.png")
    ax = fig.add_subplot(1, 1, 1)
    imgplot = plt.imshow(image)
    plt.show()

    print test_file
    a = raw_input('Next plot?\n')
    if a == "1":
        print "Do something..I've skipped these details"
    plt.clf()

plt.close()
3
  • While I can't answer the question, would it be feasible to use an ipython notebook for this project instead of raw input? That would still let you plot inline in real time. Commented Oct 2, 2014 at 15:46
  • I have never used ipython notebook. I have many images (thousands) to look through would you notebook be advisable for this? Commented Oct 2, 2014 at 15:54
  • IMO it makes more sense. You can store the images that you want to keep (if any) in the notebook as you go. Here's an example that shows plots/images in notebook: nbviewer.ipython.org/github/vanzaj/pyconsg2013-tut/blob/master/… Protip: if the last line of a notebook cell is a plot/image call, the type of object (e.g. AxesSubplot) is printed out. End your final call in the cell with a semicolon to suppress this. EG: plt.plot(x,y); Commented Oct 2, 2014 at 16:09

1 Answer 1

16

With recent version of matplotlib, you can use the call plt.show(block=False) to open the matplotlib window non-blocking.

Sign up to request clarification or add additional context in comments.

2 Comments

When I change to the above line with block=False, the plots don't actually display anymore.
@Hephaestus , use plt.pause(0.1)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.