Python 在matplotlib中设置matshow函数的动画
我有一个矩阵,它与时间有关,我想把进化过程画成动画 我的代码如下:Python 在matplotlib中设置matshow函数的动画,python,animation,matplotlib,Python,Animation,Matplotlib,我有一个矩阵,它与时间有关,我想把进化过程画成动画 我的代码如下: 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 #L
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\u只能在缓存渲染的初始绘制之后使用艺术家。我不知道这个错误是从哪里来的,也不知道如何解决它。
我读过,也读过,但仍然不知道为什么我的代码不起作用
非常感谢您的帮助,谢谢 您非常接近一个有效的解决方案。要么改变
plot = plt.matshow(data[0])
到
或使用
plot = plt.imshow(data[0])
相反
这里使用plt.matshow(数据[0])
的问题是,如果fignum
参数留空(即默认情况下等于None
)。
由于调用了fig=plt.figure()
,并且fig
被传递到FuncAnimation
,因此您最终会得到两个图形,一个图形的结果是plt.matshow
,另一个空白图形由FuncAnimation
绘制。FuncAnimation
正在绘制的图形找不到初始图形,因此会引发
AttributeError: draw_artist can only be used after an initial draw which caches the render
AttributeError: draw_artist can only be used after an initial draw which caches the render