动画MatPlotLib帧更新错误



我正在尝试制作一个动画情节的理论猎鹰9灾难轨迹由于发动机故障。但是,我的代码如下所示:

import numpy as np
from scipy.integrate import solve_ivp
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
def rocket(t, state):
# state = [x, y, phi, vx, vy, dphi/dt]
# dstate = [vx, vy, dphi/dt, ax, ay, ddphi/dt2]
dstate = np.zeros(6)
dstate[0:3] = state[3:]

if t > 60:
dstate[3] = 898.94 * 1000 * 7 * np.cos(state[2]) / 550000
dstate[4] = (898.94 * 1000 * 7 * np.sin(state[2]) / 550000) - 9.81
dstate[5] = - 0.01109404141387943 
# dstate[5] = (2 * 1.5 *  np.cos(np.pi / 8) * 898.94 * 1000) / ((550000 * 70 ** 2) / 12)
else:
dstate[3] = 898.94 * 1000 * 9 * np.cos(state[2]) / 550000
dstate[4] = (898.94 * 1000 * 9 * np.sin(state[2]) / 550000) - 9.81
dstate[5] = 0
return dstate
sol = solve_ivp(rocket, [0, 150], [0, 0, np.pi / 2, 0, 0, 0], t_eval = np.linspace(0, 150, 151))
plt.plot(sol.y[0] / 1000, sol.y[1] / 1000)
'''
print(len(sol.y[0]))
print()
print(len(np.linspace(0, 150, 151)))
'''
fig, ax = plt.subplots()
x_pos, y_pos = [], []
ln, = ax.plot([], [])
def init():
ax.set_xlim(-5, 10)
ax.set_ylim(0, 25)
ax.set_xlabel("x Position (km)")
ax.set_ylabel("y Position (km)")
ax.set_title("Falcon 9 Asymmetric Engine Failure Trajectory")
return ln,
def update(frame):
x_pos.append(sol.y[0][frame])
y_pos.append(sol.y[1][frame])
ln.set_data(x_pos, y_pos)
return ln,
ani = FuncAnimation(fig, update, frames = np.linspace(0, 150, 151), init_func = init, blit = True)
plt.show()

产生以下终端错误:

jacobivanov@Jacob-Ivanovs-MacBook-Air Lab 6 % /opt/homebrew/bin/python3 "/Users/jacobivanov/Desktop
/Falcon 9 Disaster Trajectory.py"
Traceback (most recent call last):
File "/opt/homebrew/lib/python3.10/site-packages/matplotlib/backend_bases.py", line 1193, in _on_timer
ret = func(*args, **kwargs)
File "/opt/homebrew/lib/python3.10/site-packages/matplotlib/animation.py", line 1404, in _step
still_going = super()._step(*args)
File "/opt/homebrew/lib/python3.10/site-packages/matplotlib/animation.py", line 1097, in _step
self._draw_next_frame(framedata, self._blit)
File "/opt/homebrew/lib/python3.10/site-packages/matplotlib/animation.py", line 1116, in _draw_next_frame
self._draw_frame(framedata)
File "/opt/homebrew/lib/python3.10/site-packages/matplotlib/animation.py", line 1743, in _draw_frame
self._drawn_artists = self._func(framedata, *self._args)
File "/Users/jacobivanov/Desktop/Falcon 9 Disaster Trajectory.py", line 50, in update

值得注意的是,该块在终端中可能重复了151次。问题似乎在于我们如何在更新过程中引用列表,因为下面的替换会导致相同的错误:

a = np.linspace(0, 150, 151)
b = np.linspace(0, 150, 151)
def update(frame):
'''
x_pos.append(sol.y[0][frame])
y_pos.append(sol.y[1][frame])
'''
x_pos.append(a[frame])
y_pos.append(b[frame])
ln.set_data(x_pos, y_pos)
return ln,
ani = FuncAnimation(fig, update, frames = np.linspace(0, 150, 151), init_func = init, blit = True)
plt.show()

帧的长度和解出的位置具有相同的索引长度,因此不存在不匹配。否则,我不确定我的错误在哪里。

设置求值"frames = np。Linspace (0, 150, 151)",它设置一个数组[0.0,1.0,2.0…, 150.0]。索引只能是整数,而不是浮点数,因此,当您想执行"a[frame]"时,会抛出错误。即一个[1.0]。只需在索引中添加一个int()函数。

相关内容

最新更新