2

I have some trouble to animate a 2D Array:

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 to animate this array like this: https://matplotlib.org/examples/animation/dynamic_image.html

Thanks have a nice weekend.

Mr. T
  • 11,141
  • 9
  • 27
  • 51
pats
  • 113
  • 1
  • 1
  • 7
  • 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

2

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
  • 1,732
  • 1
  • 13
  • 36
pats
  • 113
  • 1
  • 1
  • 7