15

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().

1 Answer 1

15

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)
Sign up to request clarification or add additional context in comments.

4 Comments

This got me a step closer. When I run your code above, it opens a figure at the start of execution and draws the final plot only at the end of execution (rather than having a dot appear each time I hit return). I'm running python 2.7 in spider on windows 7, if that's of any consequence.
I don't know. I have tested it on both linux and windows7 and it works. This could maybe come from your version of matplotlib. I believe that the syntax may change between different versions. I am using matplotlib 1.2.0
I found out that if you do a plt.pause() in there, it gives the figure a chance to redraw before continuing execution. Other methods of inserting a wait don't do this.
Is there a "pause until drawing everything"? 0.1 is way too long, and not long enough, depending on the plot. The canvas methods don't seem to wait for everything.

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.