I want to make a figure and then make an animation above it, I could do it, However, when I save the animation with anim.save ('Trayectories.avi'), only the figure I graph at the beginning is saved, but the animation above the figure is not saved. Below I show you my code:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import animation
fig, ax = plt.subplots()
line1, = ax.plot([], [], 'o-w', lw=2)
line2, = ax.plot([], [], 'o-w', lw=2)
datos = np.loadtxt("datos_sorteados.dat", unpack=False)
inp = np.loadtxt("Inputs.dat", unpack=False)
w = np.loadtxt("salidas.dat", unpack=False)
w_ini = np.loadtxt("salidas_antes_iterar.dat", unpack=False)
n1 = np.loadtxt("neu1.dat", unpack=False)
n2 = np.loadtxt("neu2.dat", unpack=False)
plt.plot(datos[0,:], datos[1,:], '.r', lw= 1, label = r'$Datos \ sorteados \
al \ azar$')
plt.plot( inp[0,:], inp[1,:], '.b', lw= 1, label = r'$Entradas \ de \ la \ red$')
step = 10
plt.plot( n1[0,::step], n1[1,::step], '-c', lw= 1, label = r'$Trayectoria$')
plt.plot( n2[0,::step], n2[1,::step], '-c', lw= 1)
plt.plot( w_ini[:,0], w_ini[:,1], '*m', lw= 1, label = r'$Pesos \ de \ la \ red \ antes \ de \ iterar$')
plt.plot( w[:,0], w[:,1], 'ok', lw= 1, label = r'$Pesos \ de \ la \ red \ luego \ de \ iterar$')
plt.xlabel(r'$ \xi_1 $', fontsize='xx-large')
plt.ylabel(r'$ \xi_2 $', fontsize='xx-large')
plt.legend(loc="upper right", title="")
plt.title(r'$\sigma=0.4$', fontsize='xx-large')
def init():
ax.set_xlim((-1.1,1.1))
ax.set_ylim( (0,1.7))
line1.set_data([], [])
line2.set_data([], [])
return line1, line2,
def animate(i):
line1.set_data(n1[0,i+1], n1[1,i+1])
line2.set_data(n2[0,i+1], n2[1,i+1])
return line1, line2,
anim = animation.FuncAnimation(fig, animate, init_func=init,
frames=len(n1[0,:])-1, interval=5, blit=True)
plt.grid()
anim.save('Trayectorias.avi', fps=10)
plt.show()