I have a matrix which is time-depedent and I want to plot the evolution as an Animation.
My code is the following:
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()
However, when I run it I always obtain the following error: draw_artist can only be used after an initial draw which caches the render
. I don't know where this error come from and no idea on how to solve it.
I have read this answer and also this article but still don't know why my code is not working.
Any help is appreciated, thank you!