1

Say I have a function plot(I) to plot an image like

def plot(I):
    plt.imshow(I)
    time.sleep(0.5)
    plt.show(block=False)

And in my main program, I have a loop to update I like

if __name__ == "__main__":
    I=some_input
    for i in range(300):
        I=update(I)
        plot(I)

I want to display the updated images like a gif file but the code above failed like the plot does not update and I have to close the window so that it can be updated. Is there any way to achieve what I want like display images consecutively with matplotlib.

3
  • 1
    show() is a blocking function. see also: stackoverflow.com/questions/11140787/closing-pyplot-windows/… Commented Mar 19, 2015 at 3:17
  • Also, check out making an animation with matplotlib (which happens after you make a series of images) or, if you're just monitoring at stuff on the fly, you could savefig to disk and use your file browser to keep an eye on things. Commented Mar 19, 2015 at 6:25
  • I used plt.show(block=False) so it is a unblocking function in my code. Making animation for some weird reason does not work on Mac. I cannot even run the example code of animation of matplotlib. Commented Mar 19, 2015 at 17:29

1 Answer 1

2

I got it now. This should do the trick.

def plot(I):
    plt.imshow(I,'gray')
    plt.show(block=False)
    plt.pause(0.5) 
    plt.clf()

This will plot I every 0.5 second.

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

Comments

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.