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

2
  • The answer is in the example you posted - you need to use FuncAnimation provided with a callback function to update the plot you made. Commented May 18, 2018 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. Commented May 18, 2018 at 15:37

1 Answer 1

5

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()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.