I'm generating an animation with matplotlib, but it simply isn't creating the GIF. It remains only a static and white image, as shown in the figure below:
How can I generate an animation so that it appears dynamically? Another question is how can I set the label "Tempo" (time) for all the graphs (A general legend for everyone, since it's the same)?
The code I use to generate the animation is as follows:
N_plots = int(900 / 20)
ri = 0.00001
rf = 15
radius = 0.15
dt = 0.0001
NewtonG = 6.67408*10**(-8)
LightC = 2.99792458*10**10
rho_dim = 2.7 * (10 ** (14))
fig, ((axF, axS), (axH, axHamilt)) = plt.subplots(2, 2, gridspec_kw={'hspace': 0.4, 'wspace': 0.3})
graphF, = axF.plot([], [], label = 'Tempo: 0 s')
graphS, = axS.plot([], [], label = 'Tempo: 0 s')
graphH, = axH.plot([], [], label = 'Tempo: 0 s')
graphHamilt, = axHamilt.plot([], [], label = 'Tempo: 0 s')
L = plt.legend(loc="upper right")
axF.set_ylim(-1.5, 1.5)
axF.set_xlim(ri, rf)
axF.grid()
axS.set_ylim(-0.5, 0.5)
axS.set_xlim(ri, rf)
axS.grid()
axH.set_ylim(-0.3, 0.3)
axH.set_xlim(ri, 1.1 * radius)
axH.grid()
axHamilt.set_ylim(-0.5, 0.5)
axHamilt.set_xlim(ri, rf)
axHamilt.grid()
axHamilt.set_title('Hamiltonian X r')
axH.set_title('H x r')
axF.set_title('F X r')
axS.set_title('S X r')
def animate(i):
lab = 'Tempo: ' + str(round(dt + i * dt * Nsave * (( LightC / (NewtonG * rho_dim) ** (1 / 2) ) / (LightC)), 4)) + ' s'
graphF.set_data(r, Fevol[i])
graphS.set_data(r, Sevol[i])
graphH.set_data(r, Hevol[i])
graphHamilt.set_data(r, Hamiltevol[i])
graphS.set_color("darkorange")
graphH.set_color("forestgreen")
graphHamilt.set_color("red")
L.get_texts()[0].set_text(lab)
return graphF, graphS, graphH, graphHamilt
ani = FuncAnimation(fig, animate, frames=N_plots, interval=100)
ani
A portion of the data is given by (Since 1000 points are generated, I have cut it to provide as an example.):
r = [1.00000000e-05, 2.51645972e-04, 4.93291945e-04, 7.34937917e-04,
9.76583890e-04, 1.21822986e-03, 1.45987583e-03, 1.70152181e-03,
1.94316778e-03, 2.18481375e-03]
Fevol = [[ 5.11496045e-45, 8.15102513e-41, 6.13980179e-40,-4.30628127e-11,
-3.89519864e-11, -3.45608037e-11],[ 2.03678046e-47, 3.24574332e-43,
2.44487293e-42,-1.57997251e-10, -1.49417738e-10, -1.40558032e-10],
[-9.13815280e-46, -1.45622461e-41, -1.09690871e-40,-3.44502060e-10,
-3.31446469e-10, -3.18110822e-10]]
Sevol = [[ 4.01126611e-40, 6.39221577e-36, 4.81496956e-35, 4.78940796e-06,
4.78920837e-06, 4.78900809e-06], [-5.54576762e-41, -8.83754463e-37,
-6.65692616e-36, 4.79261266e-06, 4.79241231e-06, 4.79221192e-06],
[ 6.66114341e-41, 1.06149691e-36, 7.99578035e-36, 4.79582115e-06,
4.79562050e-06, 4.79541987e-06]]
Hevol = [[ 5.01995518e-44, 3.17892153e-41, 1.22154055e-40, 0.00000000e+00,
0.00000000e+00, 0.00000000e+00], [ 1.67719339e-44, 1.06209438e-41,
4.08123113e-41, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00],
[-1.81140980e-45, -1.14708786e-42, -4.40782924e-42, 0.00000000e+00,
0.00000000e+00, 0.00000000e+00]]
Hamiltevol = [[ 9.90241594e-08, -3.07539672e-31, -5.13492241e-31,
4.69149764e-14, 4.69308735e-14, -1.19340169e-05],
[ 9.90241594e-08, -3.07539672e-31, -5.13492241e-31,
4.69149764e-14, 4.69308735e-14, -1.19340169e-05],
[ 9.90241594e-08, -3.07539672e-31, -5.13492241e-31,
4.69149764e-14, 4.69308735e-14, -1.19340169e-05]]
animate
function you doset_data(r, Fevrol[i])
.r
is a list of points butFevrol[i]
is a single point. What are you trying to do?