FuncAnimation仅打印第一张图像 [英] FuncAnimation printing first image only
问题描述
我正在尝试使用第一个维度作为时间对python中的3D数组进行动画处理。
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()
我想使用下面的代码,但是在FuncAnimation的任何地方都没有看到匿名函数。除了未设置 mat
并设置初始轴
以外,它会产生相同的结果。
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()
推荐答案
代码与在matplotlib动画中发现的任何示例之间的主要区别是,您实际上并未存储 FuncAnimation
。
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(...)
这篇关于FuncAnimation仅打印第一张图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!