# Animation and imshow

loop through the data and call clf():

from pylab import *
from numpy import *

ion()
hold(False)

frame1 = zeros((200, 200))
frame1[20:40, 20:40] = 255

frame2 = zeros((200, 200))
frame2[20:40, 30:50] = 255

frame3 = zeros((200, 200))
frame3[20:40, 40:60] = 255

frame4 = zeros((200, 200))
frame4[20:40, 50:70] = 255

frame5 = zeros((200, 200))
frame5[20:40, 50:70] = 255

data=[frame1,frame2,frame3,frame4,frame5]

for frame in data:
m=imshow(frame)
m.set_data(frame)
clf()
show()
#close()

···

Message: 2
Date: Tue, 7 Oct 2008 15:10:30 +0200
From: "Alexander Borghgraef" <alexander.borghgraef.rma@...287...>
Subject: [Matplotlib-users] Animation and imshow
To: matplotlib-users@lists.sourceforge.net
Message-ID:
<9e8c52a20810070610t5204240dq616e8fa2cc1d517b@...288...>
Content-Type: text/plain; charset=ISO-8859-1

I'm trying to figure out how to do animated graphics in pylab using
imshow, so I made this little 'hello world' equivalent showing a
moving square over two frames.
Problem is I have to call draw twice to refresh the image. Anyone can
explain why this is so (and how to do this more elegantly)? This is
the code:

from pylab import *
from numpy import *

ion()
hold(False)

frame1 = zeros((200, 200))
frame1[20:40, 20:40] = 255

frame2 = zeros((200, 200))
frame2[20:40, 30:50] = 255

m = imshow(frame1)
draw() # shows frame1
m.set_data(frame2)
draw() # still shows frame1!!!
draw() # and now it shows frame2!??

--
Alex Borghgraef

--
"When you think of the long and gloomy history of man, you will find far
more hideous crimes have been committed in the name of obedience than
have been committed in the name of rebellion". C.P.Snow,
"Either-Or" (1961)

The clf call seems quite wrong:
it flashes horribly and I get good
behavior without it (once I add
a call to sleep). What am I
missing?

Alan Isaac

PS The following almost works (i.e., works,
but then exits with an error).

import matplotlib.pyplot as plt
from time import sleep
from numpy import zeros

plt.ion()
plt.hold(False)

data = list()
slices = list()
for i in range(5):
frame = zeros( (200,200) )
frame[20:40,10*i:10*i+20] = 255
data.append(frame)

fig1 = plt.figure(1)
ax1 = fig1.gca()
for frame in data:
plt.imshow(frame, axes=ax1, animated=True)
sleep(0.2)

···

On 10/7/2008 6:23 PM Michael apparently wrote:

loop through the data and call clf():

If you do this, you can skip the set_data call, it works quite well

···

On Wed, Oct 8, 2008 at 12:23 AM, Michael <mnandris@...2197...> wrote:

loop through the data and call clf():

from pylab import *
from numpy import *

ion()
hold(False)

frame1 = zeros((200, 200))
frame1[20:40, 20:40] = 255

frame2 = zeros((200, 200))
frame2[20:40, 30:50] = 255

frame3 = zeros((200, 200))
frame3[20:40, 40:60] = 255

frame4 = zeros((200, 200))
frame4[20:40, 50:70] = 255

frame5 = zeros((200, 200))
frame5[20:40, 50:70] = 255

data=[frame1,frame2,frame3,frame4,frame5]

for frame in data:
m=imshow(frame)
m.set_data(frame)
clf()
show()
#close()

--
Alex Borghgraef