FuncAnimation printing first image only

2019-08-19 06:42发布

I'm trying to animate a 3D array in python using the first dimension as time.

I'm not sure where I am going wrong, as I recieve no error with this code. But my animation is stationary, stuck on the first page of the array.

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

array = np.random.random(size=(10, 20, 30))
empty = np.zeros(array[0].shape)

fig = plt.figure()
mat = plt.imshow(empty)
def func(i):
    mat.set_data(array[i])
    return mat
frames = len(array)
FuncAnimation(fig, func, frames)
plt.show()

I'd like the use the below code but I haven't seen an annonymous function used anywhere with FuncAnimation. It produces the same result except mat is not created setting the initial axes.

fig = plt.figure()
func = lambda i: plt.imshow(array[i])
frames = len(array)
FuncAnimation(fig, func, frames)
plt.show()

1条回答
欢心
2楼-- · 2019-08-19 07:29

The main difference between your code and any example you find on matplotlib animations is that you do not actually store the FuncAnimation. Depending on how you run things it would then directly be garbage-collected.

ani = FuncAnimation(...)
查看更多
登录 后发表回答