我正在尝试使用动画。函数中的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时,代码仍然有效,但绿色";当前位置";不再自动渲染。它只在我更改三维绘图视图的透视图时显示,不再引导轨迹。
错误是因为我没有更新值,而是再次渲染了整行,我猜这将覆盖pos
艺术家对象的渲染。当我转而更改数据时,渲染工作正常:
def orbit_anim(frame, ax, r, orb, pos):
orb.set_data(r[:frame+1, 0], r[:frame+1, 1])
orb.set_3d_properties(r[:frame+1, 2], 'z')
pos.set_data(r[frame, 0], r[frame, 1])
pos.set_3d_properties(r[frame, 2], 'z')
return orb, pos