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.

zhang rui
  • 81
  • 1
  • 4
  • 1
    show() is a blocking function. see also: http://stackoverflow.com/questions/11140787/closing-pyplot-windows/11141305#11141305 – ajdigregorio Mar 19 '15 at 03: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. – cphlewis Mar 19 '15 at 06: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. – zhang rui Mar 19 '15 at 17:29

1 Answers1

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.

zhang rui
  • 81
  • 1
  • 4