On Mon, Sep 20, 2010 at 8:14 PM, Brian Blais <bblais@bryant.edu> wrote:
On Sep 20, 2010, at 7:43 PM, Benjamin Root wrote:

> On Mon, Sep 20, 2010 at 5:12 PM, Brian Blais <bblais@bryant.edu> wrote:

> I am trying to do some simple calculations in a loop, and draw a plot periodically within the loop, and the drawing is not updating.  I'm using the Enthought Python Distribution which is using Matplotlib 0.99.3 with python 2.6.5 on Snow Leopard, OSX 10.6.4, and am running it in ipython with the -pylab flag (and I've tried with the -wthread flag too).  A sample piece of code below.  It is actually drawing, because when I control-C to stop, it shows the plot.
> from pylab import *
> from numpy import *
> import sys
> def dot():
>    sys.stdout.write('.')
>    sys.stdout.flush()
> def busy_loop():
>    for i in range(1000):
>        r=rand(100,100)
>    return r
> for t in range(1000):
>    r=busy_loop()
>    clf()
>    imshow(r,interpolation='nearest',cmap=cm.gray)
>    draw()
>    show()
>    dot()
> First, I would suggest using time.sleep() to do your busy loop:
> >>> import time
> >>> time.sleep(0.1)

I tried that before (even upping to 1 second)...no dice.

When you say 'no dice', do you mean that it didn't pause execution at all, or that it didn't solve your problem?  Note that I wasn't implying that it would solve your problem, only that it was probably a cleaner way to write code and it gives you explicit control over how long to wait regardless of which computer you are on.
> Second, you have the show() function within the loop.  Call the show() function only once (in interactive mode), and draw() can be used to update the graph.

also done...no effect.

Well, in any case, you should only have a single show() call for this use case. draw() is used by show() to actually perform the rendering.  Whatever issue you are having, it isn't from you calling show() only once.

>  Also note that some plotting functions return objects that have a function like "update_data" that would allow you to modify the plot without having to call clf().

sure, but that take more effort, and I don't really care about a slow down, because most of my time is spent in the busy loop.  I do care about it displaying *at all*, which is the problem.  I am familiar with the efficient way of animating, but for most things I want to focus on the computation and then display here and there, so I want the shortest, clearest plotting code without any optimizations.  if I call plot commands, and then a draw, I expect it to actually pause right there until it is done drawing, and then continue...it's not doing that.

And that's the way it is supposed to work.
another wrinkle:  If I run ipython -pylab, and then run my script like:

run do_my_script

I get no plot showing (an empty figure window shows, with a busy process "rainbow spiral).  Now, I control-C and the plots come up and my script stops.  I then type again:

run do_my_script

it works!  once the figure is drawn once, it seems to work just fine.  the *first* time it doesn't display, and requires a control-C.  weird!

can anyone reproduce this?


This might be an issue with the somewhat older version of matplotlib that is with EPD.  I know there have been some significant fixes with the interactivity of plots.  But I haven't seen this much of an issue.

Give this a shot... run ipython -pylab and create some figure by hand,  call .show(), and then do something else to the plot.  No calls to draw() and only one initial call to show() to get the figure window up, I am just curious if the figure updates itself on its own.

Ben Root