0

I am trying to use animation.FuncAnimation within a function. The goal is to make an animated demonstration of an orbit in a two body model.

The function that calls the animation is setup as follows:

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 adds the plot of a sphere and a starting point. orbit_anim looks as follows:

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

The code works as intended when blit is false. The green "Current Position" dot leads the orbital trajectory line and is rendered. However, when blit is set to true, the code still works, but the green "Current position" is no longer automatically rendered. It only shows up when I change the perspective of the 3d plot view and no longer leads the trajectory.

Abhilekh
  • 13
  • 3

1 Answers1

0

The error was caused because instead of updating the value, I rendered the entire line again, which I am guessing would override the render of the pos artist object. The rendering worked when I switched into changing the data instead:

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
Abhilekh
  • 13
  • 3