为什么matplotlib动画没有显示出来?

5lhxktic  于 2023-08-06  发布在  其他
关注(0)|答案(1)|浏览(152)

我正在用matplotlib生成一个动画,但它并没有创建GIF。它仍然只是一个静态和白色的图像,如下图所示:


的数据
如何生成动画以使其动态显示?另一个问题是我如何为所有的图表设置标签“克里思”(时间)(因为它是相同的,所以每个人都有一个通用的图例)?
我用来生成动画的代码如下:

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

字符串
数据的一部分由下式给出(由于生成了1000个点,因此我将其剪切以作为示例。):

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

fsi0uk1n

fsi0uk1n1#

你的代码有几个问题。
1.你的Y极限被打破了。我通过确定数据的限制来解决这个问题。
1.你的X限制被关闭了。我还通过使用r数据的限制解决了这个问题。
1.我通过使用suptitle并在每次迭代时更新文本来添加时间(您没有提供Nsave,所以我只使用i*dt)。
1.您的r数据长度为10,但其余数据的每行长度为6,因此我从r中删除了最后4项。希望实际数据的形状匹配。

  1. N_plots需要基于数据中的行数,您给出了3。
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)

字符串


的数据
所有的子图都在变化,但是右边两个的值太小了,以至于你不能真正看到运动。

相关问题