Below is an example Python script that uses Matplotlib’s 3D plotting and animation tools to show a simplified interplanetary mission. In this example, we assume:
The Sun is at the origin.
’s orbit is a circle of radius 1.
’s orbit is a circle of radius 1.524.
A Hohmann‐transfer “leg” from to is modeled as an ellipse (with at perihelion and at aphelion) computed via
The Sun is at the origin.
’s orbit is a circle of radius 1.
’s orbit is a circle of radius 1.524.
A Hohmann‐transfer “leg” from to is modeled as an ellipse (with at perihelion and at aphelion) computed via
The outbound transfer is taken as the half–ellipse fromθ=0\theta=0θ=0(Earth) toθ=π\theta=\piθ=π(Mars).
The return leg is the same ellipse but “traced” fromθ=π\theta=\piθ=).
For a bit of three‐dimensional flavor we add a modest “lift” (using a sine profile) so the spacecraft arcs slightly out of the orbital plane during each transfer.
The outbound transfer is taken as the half–ellipse fromθ=0\theta=0θ=0(Earth) toθ=π\theta=\piθ=π(Mars).
The return leg is the same ellipse but “traced” fromθ=π\theta=\piθ=).
For a bit of three‐dimensional flavor we add a modest “lift” (using a sine profile) so the spacecraft arcs slightly out of the orbital plane during each transfer.
The animation is divided into four phases (all in one 300‑frame cycle):
Leg 1 (frames 0–99):Spacecraft departs (at(1,0,0)(1,0,0)(1,0,0)) along the outbound ellipse.
Mars Dwell (frames 100–149):The spacecraft “lands” and stays on .
Leg 2 (frames 150–249):The spacecraft departs and follows the return ellipse back to .
Earth Dwell (frames 250–299):The spacecraft remains on .
Leg 1 (frames 0–99):Spacecraft departs (at(1,0,0)(1,0,0)(1,0,0)) along the outbound ellipse.
Mars Dwell (frames 100–149):The spacecraft “lands” and stays on .
Leg 2 (frames 150–249):The spacecraft departs and follows the return ellipse back to .
Earth Dwell (frames 250–299):The spacecraft remains on .
and are also shown as moving markers along their circular orbits. (The orbital phases have been “tuned” so that at the critical transfer endpoints the planet marker and the transfer ellipse coincide.)
You can run the code below (after installing NumPy and Matplotlib) to see the animated 3D plot:
pythonCopyimport numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
from mpl_toolkits.mplot3d import Axes3D # registers the 3D projection
--- PARAMETERS ---
Orbital radii (in arbitrary AU units)
r_earth = 1.0
r_mars = 1.524
Hohmann transfer parameters (for -to- leg)
a = (r_earth + r_mars) / 2.0
e = (r_mars - r_earth) / (r_mars + r_earth)
def update(frame):
# Update spacecraft position from precomputed path
x_sc, y_sc, = sc_path[frame]
sc_marker.set_data([x_sc], [y_sc])
sc_marker.set_3d_properties([z_sc])
code
# For a simple animation of the planets along their orbits, we let:
# Earth completes one orbit in 250 frames (so at frame 0 and 250, <REDACTED> is at (1,0,0))
earth_angle = 2 * np.pi * (frame / 250)
x_earth = r_earth * np.cos(earth_angle)
y_earth = r_earth * np.sin(earth_angle)
z_earth = 0
earth_marker.set_data([x_earth], [y_earth])
earth_marker.set_3d_properties([z_earth])
# <REDACTED>: adjust its angular speed and phase so that at frame 100 it is at the <REDACTED> landing point.
# We choose <REDACTED>'s motion so that at frame 100, its polar angle is pi.
# Let mars_angle = (pi/100)*frame + offset. Solve for offset at frame=100: (pi/100)*100 + offset = pi ⟹ offset = 0.
# For more visual interest, we add an offset so that at frame 0 <REDACTED> is not exactly aligned with <REDACTED>.
mars_angle = (np.pi / 100) * frame + 0.6 * np.pi
x_mars = r_mars * np.cos(mars_angle)
y_mars = r_mars * np.sin(mars_angle)
z_mars = 0
mars_marker.set_data([x_mars], [y_mars])
mars_marker.set_3d_properties([z_mars])
return <REDACTED>, earth_marker, mars_marker
--- CREATE THE ANIMATION ---
ani = FuncAnimation(fig, update, frames=total_frames, interval=50, blit=True)
plt.show()
How It Works
Trajectory Computation:The code first computes the outbound (Earth → ) and return ( → Earth) transfer paths using the polar form of an ellipse. A slight sinusoidal “lift” (the z‑component) is added to emphasize the 3D view.
Planetary Orbits:The static dashed lines show the circular orbits of (blue) and (red). In addition, moving markers for and update every frame. Their angular speeds have been “tuned” so that at key frames the spacecraft’s endpoint coincides with the appropriate planet.
Animation Phases:The animation runs over 300 frames. During the first 100 frames the spacecraft moves along the outbound ellipse. It then “dwells” on for 50 frames, follows the return ellipse for 100 frames, and finally dwells on for the last 50 frames.
Visualization:A 3D plot is created with labeled axes and a legend. The Sun is shown as a yellow circle at the center.
Trajectory Computation:The code first computes the outbound (Earth → ) and return ( → Earth) transfer paths using the polar form of an ellipse. A slight sinusoidal “lift” (the z‑component) is added to emphasize the 3D view.
Planetary Orbits:The static dashed lines show the circular orbits of (blue) and (red). In addition, moving markers for and update every frame. Their angular speeds have been “tuned” so that at key frames the spacecraft’s endpoint coincides with the appropriate planet.
Animation Phases:The animation runs over 300 frames. During the first 100 frames the spacecraft moves along the outbound ellipse. It then “dwells” on for 50 frames, follows the return ellipse for 100 frames, and finally dwells on for the last 50 frames.
Visualization:A 3D plot is created with labeled axes and a legend. The Sun is shown as a yellow circle at the center.
You can adjust parameters (such as the number of frames, dwell times, or the “lift” amplitude) to refine the visualization. This example provides a basic framework to visualize an –– mission in 3D with animation.