Hi,
I have a problem with draw() to do simple animations of the contents of arrays in matplotlib.
I was trying to use the idea in the animations cookbook (http://www.scipy.org/Cookbook/Matplotlib/Animations)
to animate some “random walkers”, but found that the animation did not work.
A minimal example of the problem is given by changing the first recipe in the cookbook to just draw random arrays.
This version correctly animates as expected:
from pylab import *
import time
ion()
tstart = time.time() # for profiling
x = rand(100)
y = rand(100)
line, = plot(x,y, ‘,’)
for i in arange(1,200):
x = rand(100)
y = rand(100)
line.set_data(x,y)
draw() # redraw the canvas
Now, however, changing the “x =” and “y =” lines as follows:
x[:] = rand(100)
y[:] = rand(100)
so that they are modified in place, rather than creating new arrays, no longer animates anything.
I am using version 0.99 on linux (Kubuntu 9.10).
The same behaviour is found from within ipython -pylab or from the command line with python.
In my real application, I wish to use this as a simple way to animate a collection of random walkers.
I thus have an array of positions which is updated at every step, and this is what I want to animate,
which is the reason why I tried the array updating step above.
So far, my code is as follows:
from pylab import *
ion()
N = 1000
pos = zeros((N,2))
figure(figsize=(8,8))
points, = plot(pos[:,0], pos[:,1], ‘,’)
axis([-20,20,-20,20])
for t in range(1000):
pos += uniform(-1,1,N*2).reshape(N,2)
points.set_data(pos[:,0].copy(), pos[:,1].copy())
draw()
The “.copy()” are an attempt at creating new arrays. Nonetheless, there is no animation.
And if I put
points._x
in ipython, then it still has all zeros! Apparently the .set_data() hasn’t done anything.
Any help at getting this animation to work are greatly appreciated!
Thanks and best wishes,
David.