I’m trying to create a 3D animation of the moon’s orbit around the Earth. Each point in the time vector (‘times’) that is used to create the orbit data is not equally spaced, however, I would like to have each frame in the animation be equally spaced in time so that the orbit is continuous. (The time vector is generated using an integration technique that cannot keep the interval between times constant.) I figured that the best way to do this would be to vary the number of points plotted per frame, but I can’t figure out how to implement that using FuncAnimation.
The code below is clearly wrong in a lot of ways, but this is the direction I was heading towards. Is there a way to use FuncAnimation to vary the number of points plotted per frame so that the time interval is constant?
# ~~~~~ANIMATION~~~~~
fig = plt.figure()
ax = fig.add_subplot(projection='3d')
# Collect animation data
data_Moon = np.array([moon_r*np.cos(times), moon_r*np.sin(times), np.zeros(len(times))])
# Initialize the first point
line_Moon, = ax.plot(data_Moon[0, 0:1], data_Moon[1, 0:1], data_Moon[2, 0:1], color='gray', label='Moon')
interval = 0.05 # Desired time interval for each frame
def next_frame(times, interval):
# Supposed to yield the number of points to plot in the current frame
t0 = 0
for ii in np.arange(len(times)):
diff = times[ii] - t0
if diff >= interval:
yield len(diff)
t0 = times[ii]
def animate(i):
line_Moon.set_data(data_Moon[0, :i * P], data_Moon[1, :i * P]) # Set the x and y positions
line_Moon.set_3d_properties(data_Moon[2, :i * P]) # Set the z position
ani = animation.FuncAnimation(fig, animate, frames=next_frame(times, interval), interval=1, repeat=True)
plt.show()
I hope I explained this well! I appreciate any help in advance.
Anna Boese is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.