3
我有一个时间依赖的矩阵,我想将动画演变为动画。matplotlib中的动画matshow函数
我的代码如下:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
n_frames = 3 #Numero de ficheros que hemos generado
data = np.empty(n_frames, dtype=object) #Almacena los datos
#Leer todos los datos
for k in range(n_frames):
data[k] = np.loadtxt("frame"+str(k))
fig = plt.figure()
plot =plt.matshow(data[0])
def init():
plot.set_data(data[0])
return plot
def update(j):
plot.set_data(data[j])
return [plot]
anim = FuncAnimation(fig, update, init_func = init, frames=n_frames, interval = 30, blit=True)
plt.show()
然而,当我运行它,我总是得到以下错误:draw_artist can only be used after an initial draw which caches the render
。我不知道这个错误来自哪里,也不知道如何解决它。 我已阅读this answer也this article但仍然不知道为什么我的代码不起作用。
任何帮助表示赞赏,谢谢!