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

def busy_loop():

   for i in range(1000):

   return r

for t in range(1000):




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!


                       Brian Blais


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:


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