pats
pats

Reputation: 143

How to animate a 2D numpy array using matplotlib?

I'm trying to animate a numpy array using matplotlib:

import numpy as np

import matplotlib.pyplot as plt
import matplotlib.animation as animation

arr = []
for i in range(100):
    c = np.random.rand(10, 10)        
    arr.append(c)

plt.imshow(arr[45])

I don't get it how I should animate an array like this: https://matplotlib.org/examples/animation/dynamic_image.html

Upvotes: 2

Views: 16044

Answers (1)

pats
pats

Reputation: 143

Oh thanks, it was easyer then I expected.

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation

fig = plt.figure()
i=0
im = plt.imshow(arr[0], animated=True)
def updatefig(*args):
    global i
    if (i<99):
        i += 1
    else:
        i=0
    im.set_array(arr[i])
    return im,
ani = animation.FuncAnimation(fig, updatefig,  blit=True)
plt.show()

Upvotes: 5

Related Questions