0

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:

Gif

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]]

2
  • In your animate function you do set_data(r, Fevrol[i]). r is a list of points but Fevrol[i] is a single point. What are you trying to do?
    – jared
    Commented Jul 17, 2023 at 0:01
  • It's true, I ended up inputting the wrong data. I will fix it, thank you! Commented Jul 17, 2023 at 1:44

1 Answer 1

0

You had a couple of issues with your code.

  1. Your y limits were off. I fixed this by determining the limits of the data.
  2. Your x limits were off. I also fixed this by using the limits of the r data.
  3. I added the time by using a suptitle and updating the text every iteration (you didn't provide Nsave, so I just used i*dt).
  4. Your r data was 10 long but each row of the rest of the data was 6 long, so I removed the last 4 items from r. Hopefully, the shapes of your actual data matches.
  5. N_plots needed to be based on the number of rows in the data, which you gave 3 of.
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.animation import FuncAnimation

plt.close("all")

r = ...
Fevol = ...
Hevol = ...
Sevol = ...
Hamiltevol = ...

N_plots = len(Fevol)
ri = min(r)
rf = max(r)
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)

axes = (axF, axS, axH, axHamilt)
data = (Fevol, Sevol, Hevol, Hamiltevol)
for ax, datum in zip(axes, data):
    ax.set_ylim(np.min(datum)*0.95, np.max(datum)*1.05)
    ax.set_xlim(ri, rf)
    ax.grid()

axH.set_title("H x r")
axF.set_title("F X r")
axS.set_title("S X r")
axHamilt.set_title("Hamiltonian X r")
title = fig.suptitle("Tempo: 0.0000 s")

graphF, = axF.plot([], [], color="darkorange")
graphS, = axS.plot([], [], color="darkorange")
graphH, = axH.plot([], [], color="forestgreen")
graphHamilt, = axHamilt.plot([], [], color="red")

fig.tight_layout()

def animate(i):
    graphF.set_data(r, Fevol[i])
    graphS.set_data(r, Sevol[i])
    graphH.set_data(r, Hevol[i])
    graphHamilt.set_data(r, Hamiltevol[i])
    title.set_text(f"Tempo: {i*dt:.4f} s")
    return graphF, graphS, graphH, graphHamilt, title

ani = FuncAnimation(fig, animate, frames=N_plots, interval=100)

All the subplots are changing, but the values are so small for the right two that you can't really see the motion.

1
  • I discovered that my original code was running fine. The problem of the GIF not showing up and only the static image was that I was running it in Jupyter and I was missing a matplotlib notebook before the code. Anyway, thank you for pointing out potential errors and for helping me with the time Commented Jul 17, 2023 at 22:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.