On Mon, Nov 2, 2009 at 10:52 PM, David Sanders <dpsanders@...> wrote:
> 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 Line2D object keeps the input data as a cache and only update it
(recache) if the new data is different than the cached one.
The problem in this particular case is that the cache is actually a
*pos* itself. And modifying the pos in place, actually modify the
cache in the Line2D object. Thus, set_data sees that the given data is
identical to the cached one, and skip the recaching.
I'm not sure what is the best approach here, and I defer the fix (or
not) to others.
Meanwhile, you can force the recaching with recache method. i.e., call
points.recache() after set_data. You don't need to make a copy also.
As a matter of fact, I think it will give you a best performance (but
not tested) if you directly update the cached data and do not call
set_data.
Note that in this particular case, pos == cache, so you actually don't
need to call get_data, but this is not a general case.
posx, posy = points.get_data(orig=True)
for t in range(100):
dx, dy = uniform(-1,1,N*2).reshape(2, N) # note the change in the shape
posx += dx
posy += dy
points.recache()
draw()
Regards,
-JJ
|