2

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

LW001
  • 2,452
  • 6
  • 27
  • 36
pats
  • 133
  • 1
  • 1
  • 8
  • The answer is in the example you posted - you need to use `FuncAnimation` provided with a callback function to update the plot you made. – jadelord May 18 '18 at 15:12
  • You need to define a figure (fig in the example), so `FuncAnimation()` knows, what to update. Then you need an update function that tells `matplotlib`, what to do with the figure. Is it a jump to the left and then a step to the right? We don't know, but the script should. – Mr. T May 18 '18 at 15:37

1 Answers1

4

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()
user213544
  • 2,046
  • 3
  • 22
  • 52
pats
  • 133
  • 1
  • 1
  • 8