Automatically update a figure in a loop

One thing I found out quickly is that I could not update a matplotlib plot in Python, at least automatically.  Then I found out this little trick.  The idea is that at the end of the loop one must put a [cci lang="python"]fig1.canvas.draw()[/cci] (obviously change the [cci lang="python"]fig1[/cci] part so that it reflects the variable name of the figure).  It works very well and seems to be quite quick for simple things.

[cc lang="python"]
fig1 = figure()

for ii in range(30):

## Clear the figure
clf()

# Do a plot that changes for each
# iteration
plot( range(ii) )

# Now set the xlimit so that it
# doesn’t change the plot
xlim((0,30))
ylim((0,30))

## Now the magic, we must update the
## canvas
fig1.canvas.draw()

[/cc]

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>