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

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




and I've set in my matplotlibrc -  backend      : TkAgg, but it doesn't work with WxAgg either.

Am I missing something?  Is this an idiom that needs to be avoided/replaced?


Any help would be great!


               thanks,

                       Brian Blais



Brian,

First, I would suggest using time.sleep() to do your busy loop:

>>> import time
>>> time.sleep(0.1)

to sleep for a tenth of a second.

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

Some of these examples in the given link might be overkill for your needs:

http://matplotlib.sourceforge.net/examples/animation/index.html

but they may be useful to better understand how animations can be done.  Also note that the next release of matplotlib will include a much easier to use module to create animations (of course, if you are daring, you could install the latest matplotlib from source).

I hope this helps!
Ben Root