I'd like to display a simple animation by calling imshow
in a for loop. Here's a demo of my problem:
import pylab,time
images = [pylab.uniform(0,255,(50,50)) for _ in xrange(40)]
pylab.ion()
timings = []
for img in images:
tic = time.time()
pylab.imshow(img)
pylab.draw()
toc = time.time()
timings.append(toc-tic)
pylab.clf()
pylab.plot(timings)
pylab.title('elapsed time per iteration')
pylab.ioff()
pylab.show()
Note that I generate the images before running the loop, and that the only parts I time are the imshow
and draw
functions. I'm getting results that look like this:
How can I avoid this slowdown?