Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do I redraw an image using python's matplotlib?

What I am trying to do seems to be fairly straightforward, but I'm having a heck of a time trying to get it to work. I am simply trying to draw an image using imshow and then re-draw it periodically as new data arrives.

I've started out with this:

fig = figure()
ax = plt.axes(xlim=(0,200),ylim=(0,200))
myimg = ax.imshow(zeros((200,200),float))

Then I'm assuming I can call set_data like this to update the image:

myimg.set_data(newdata)

I've tried many other things, for example I've called ax.imshow(newdata) instead or I've tried using figure.show() after set_data().

like image 401
kjgregory Avatar asked Jan 05 '14 17:01

kjgregory


1 Answers

You can simply call figure.canvas.draw() each time you append something new to the figure. This will refresh the plot.

from matplotlib import pyplot as plt
from builtins import input

fig = plt.figure()
ax = fig.gca()
fig.show()

block = False
for i in range(10):
    ax.plot(i, i, 'ko')
    fig.canvas.draw()
    if block: 
        input('pause : press any key ...')
    else:
        plt.pause(0.1)
plt.close(fig)
like image 75
user2660966 Avatar answered Oct 09 '22 21:10

user2660966