matplotlib imshow(): how to animate?
Asked Answered
M

2

39

i found this wonderful short tutorial on animation:

http://jakevdp.github.io/blog/2012/08/18/matplotlib-animation-tutorial/

however i cant produce an animated imshow() plot of same fashion. I tried to replace some lines:

# First set up the figure, the axis, and the plot element we want to animate
fig = plt.figure()
ax = plt.axes(xlim=(0, 10), ylim=(0, 10))
#line, = ax.plot([], [], lw=2)
a=np.random.random((5,5))
im=plt.imshow(a,interpolation='none')
# initialization function: plot the background of each frame
def init():
    im.set_data(np.random.random((5,5)))
    return im

# animation function.  This is called sequentially
def animate(i):
    a=im.get_array()
    a=a*np.exp(-0.001*i)    # exponential decay of the values
    im.set_array(a)
    return im

but i run into errors can you help me get this running? thank you in advance. best,

Mohr answered 20/6, 2013 at 11:42 Comment(1)
As a side note, it is good practice to include what errors you are getting in your question.Eolian
A
37

You're very close, but there's one mistake - init and animate should return iterables containing the artists that are being animated. That's why in Jake's version they return line, (which is actually a tuple) rather than line (which is a single line object). Sadly the docs are not clear on this!

You can fix your version like this:

# initialization function: plot the background of each frame
def init():
    im.set_data(np.random.random((5,5)))
    return [im]

# animation function.  This is called sequentially
def animate(i):
    a=im.get_array()
    a=a*np.exp(-0.001*i)    # exponential decay of the values
    im.set_array(a)
    return [im]
Amorphous answered 20/6, 2013 at 12:7 Comment(1)
This works for me in a global script, but doesn't work inside the main function. I have the init and animate functions within the main. Any suggestions?Nonbeliever
P
24

Here's a complete example:

# Usually we use `%matplotlib inline`. However we need `notebook` for the anim to render in the notebook.
%matplotlib notebook

import random
import numpy as np

import matplotlib
import matplotlib.pyplot as plt

import matplotlib.animation as animation


fps = 30
nSeconds = 5
snapshots = [ np.random.rand(5,5) for _ in range( nSeconds * fps ) ]

# First set up the figure, the axis, and the plot element we want to animate
fig = plt.figure( figsize=(8,8) )

a = snapshots[0]
im = plt.imshow(a, interpolation='none', aspect='auto', vmin=0, vmax=1)

def animate_func(i):
    if i % fps == 0:
        print( '.', end ='' )

    im.set_array(snapshots[i])
    return [im]

anim = animation.FuncAnimation(
                               fig, 
                               animate_func, 
                               frames = nSeconds * fps,
                               interval = 1000 / fps, # in ms
                               )

anim.save('test_anim.mp4', fps=fps, extra_args=['-vcodec', 'libx264'])

print('Done!')

# plt.show()  # Not required, it seems!
Punchboard answered 29/7, 2019 at 18:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.