Show consecutive images/arrays with imshow as repeating animation in python

17,964

Solution 1

I implemented a handy script that just suits your need. Try it out here

For your example:

imagelist = YOUR-IMAGE-LIST
def redraw_fn(f, axes):
    img = imagelist[f]
    if not redraw_fn.initialized:
        redraw_fn.im = axes.imshow(img, animated=True)
        redraw_fn.initialized = True
    else:
        redraw_fn.im.set_array(img)
redraw_fn.initialized = False

videofig(len(imagelist), redraw_fn, play_fps=30)

Solution 2

Copying almost exactly from your first link (and adding some comments):

hmpf = ones([4,4])
hmpf[2][1] = 0
imagelist = [ hmpf*i*255./19. for i in range(20) ]

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
fig = plt.figure() # make figure

# make axesimage object
# the vmin and vmax here are very important to get the color map correct
im = plt.imshow(imagelist[0], cmap=plt.get_cmap('jet'), vmin=0, vmax=255)

# function to update figure
def updatefig(j):
    # set the data in the axesimage object
    im.set_array(imagelist[j])
    # return the artists set
    return [im]
# kick off the animation
ani = animation.FuncAnimation(fig, updatefig, frames=range(20), 
                              interval=50, blit=True)
plt.show()

This animates as I expect

Solution 3

Are you running in an interactive python session in Spyder? If so, you may need to run

    %matplotlib qt

To make sure that the animation opens in its own window, rather than being displayed inline (it doesn't work inline).

Also, make sure that you are not falling for the calling animation.FuncAnimation inside a function issue

Share:
17,964

Related videos on Youtube

Leo
Author by

Leo

Updated on September 15, 2022

Comments

  • Leo
    Leo over 1 year

    I have calculated some results and they are in the form of 64x64 arrays. Each array is created some time after the other. I want to show these arrays one after the other, like an animation. I have tried many ways, and got none to work. Im am quite frustrated and the questions on SO regarding animations have not been able to help me get this to work. This is not the first time I try this, every time my result is the same though: I have never gotten this to work.

    The approaches I have tried:

    dynamic image

    dynamic image 2

    simple animation

    The current code I have:

    fig, ax = plt.subplots()
    def animate(i):
        return imagelist[i] 
    def init():
        fig.set_data([],[])
        return fig
    ani = animation.FuncAnimation(fig, animate, np.arange(0, 19), init_func=init,
    interval=20, blit=True)
    plt.show()
    

    Here imagelist is a list of the arrays I mention above (length 20, 0 through 19). My question is how can I get this to work?

  • Leo
    Leo over 10 years
    So I dont need an init function? This runs without errors, however I dont see an animation, just the first image.
  • tacaswell
    tacaswell over 10 years
    try removing blit=True
  • Leo
    Leo over 10 years
    does it show and animation for you? I only see the first image. hmpf = ones([4,4]), hmpf[2][1] = 0, imagelist = [ hmpf*i*255./19. for i in range(20) ] creates a list of arrays you can use.
  • Kevin Kostlan
    Kevin Kostlan over 8 years
    (python3 not sure for 2): You MUST assign animation.FuncAnimation to a variable. Removing ani = will make the animation not be executed.
  • Greg
    Greg almost 7 years
    I get "'AxesImage' object is not iterable" and "Exception in Tkinter callback" with the code provided, but this is fixed by modifying updatefig to return [im] instead of im.
  • tacaswell
    tacaswell almost 7 years
    @Greg The comma in the above code is important (and makes the returned object a length 1 tuple instead of an AxesImage).
  • Greg
    Greg almost 7 years
    @tacaswell, thanks, I overlooked that. Is there any reason to prefer an implicit tuple rather than explicitly adding () or []? Hopefully this comment thread will add the error I got to google's index of this solution, since it is easy to miss the comma or disregard as a typo if the reader is unfamiliar with that notation.
  • tacaswell
    tacaswell almost 7 years
    mostly just lazyness (1 less key!)
  • tacaswell
    tacaswell almost 7 years
    Edited to be [] for clairity.
  • trdavidson
    trdavidson over 6 years
    for me, this was key to making it work inline on a mac: rc('animation', html='html5'), then simply execute 'ani'