【发布时间】:2021-04-14 21:02:02
【问题描述】:
我正在尝试在函数中使用 animation.FuncAnimation。目标是在两体模型中对轨道进行动画演示。
调用动画的函数设置如下:
def plot_animate(r, bod_rad, steps, dt):
#Setup plot environment
fig = plt.figure(figsize=(10, 10))
ax = fig.add_subplot(111, projection='3d')
max_val = np.max(np.abs(r))
ax.set_xlim([-max_val, max_val])
ax.set_ylim([-max_val, max_val])
ax.set_zlim([-max_val, max_val])
#Plot background and body to orbit
ax = plot_bckground(ax, bod_rad)
#Setup initial position and current position
ax.plot([r[0,0]],[r[0, 1]], [r[0,2]],'ko', label='Starting Position', zorder=20)
orb, = ax.plot(r[0,0], r[0,1], r[0,2], 'k--', label='trajectory', zorder=10)
pos, = ax.plot([r[0,0]],[r[0, 1]], [r[0,2]],'go', label='Current Position', zorder=10)
#Animate trajectory
anime = animation.FuncAnimation(fig, orbit_anim, fargs=(ax, r, orb, pos),\
frames=steps, interval=dt, blit=True)
plt.legend()
plt.show()
plt_background 添加球体和起点的图。
orbit_anim 如下:
def orbit_anim(frame, ax, r, orb, pos):
#Trajectory and current position implementation to animate the satellite
orb = ax.plot(r[:frame+1, 0], r[:frame+1, 1], r[:frame+1, 2], 'k--', label='trajectory', zorder=10)
pos.set_data(r[frame, 0], r[frame, 1])
pos.set_3d_properties(r[frame, 2], 'z')
return orb
当 blit 为 false 时,代码按预期工作。绿色的“当前位置”点引导轨道轨迹线并被渲染。 但是,当 blit 设置为 true 时,代码仍然有效,但不再自动呈现绿色的“当前位置”。它仅在我更改 3d 绘图视图的视角且不再引导轨迹时显示。
【问题讨论】: