0

I want to loop over a series of images to see how they change over time. Thus, I want them plotted on the same figure. The following code works but seems to slow down after a few iterations. Does anyone know why this is happening, how to overcome it, or an alternative way to visualize these images over time?

fig, ax=pyplot.subplots(figsize=(8,6))
for i in range(n):
    ax.imshow(imageArray[i])
    fig.canvas.draw()
    time.sleep(0.2)
  • It is unclear what you precisely want to do here; can you please clarify? – Reblochon Masque Apr 06 '16 at 04:20
  • Something like this? [matplotlib imshow how to animate](http://stackoverflow.com/questions/17212722/matplotlib-imshow-how-to-animate) – Reti43 Apr 06 '16 at 04:34
  • Why don't you follow Jake's tutorial? It is very well done! :) – Reblochon Masque Apr 06 '16 at 05:33
  • The animation is getting slower because the old image isn't deleted. More and more images will be redrawn each time you call 'fig.canvas.draw(). Therefore add `ax.cla()` before the `imshow` call. The tutorial that Jake suggested doesn't need the `cla` because it sets the image directly, and will therefore be slightly faster. – titusjan Apr 06 '16 at 06:48
  • @titusjan I think that should be an answer – David Z Apr 06 '16 at 07:54
  • @david-z: you're right. I made a comment because I don't have time to verify my reply. However, I'm pretty sure this is it, so I now made an answer of it. – titusjan Apr 06 '16 at 11:15

1 Answers1

2

The animation is getting slower because the old image isn't deleted. More and more images will be redrawn each time you call fig.canvas.draw(). Therefore add ax.cla() before the imshow call. The tutorial that Jake suggested doesn't need the cla because it sets the image directly, and will therefore be slightly faster.

titusjan
  • 5,376
  • 2
  • 24
  • 43