import matplotlib.pyplot as plt
from IPython.display import HTML
import pyorbiter as orb
sim = (
orb.create_system()
.add_sun()
.add_planet("Earth", parent="Sun")
.simulate(time_step=orb.seconds_per_day, duration=orb.seconds_per_year)
)
anim = orb.animate_system(sim, fps=5, duration=2, trails=True, three_d=False)
plt.close()
HTML(anim.to_jshtml())animate_system
animate_system(
sim_data,
fps=20,
duration=10,
trails=False,
three_d=None,
path=None,
)Animate the system over time.
Plays the simulation forward. Each body is a marker that moves along its orbit frame by frame, optionally leaving a short wake behind it. This is the animated counterpart to plot_system: a moving snapshot instead of a single frozen one.
If every z value in sim_data is zero, a 2D matplotlib animation is built. If any z value is non-zero, the work is handed to animate_system_3d, which returns an interactive plotly figure. Use three_d to force one or the other.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| sim_data | DataFrame | The output of simulate_system (pandas or polars). It must have the columns id, x, y, z (meters) and time (seconds). |
required |
| fps | float | Frames per second of playback. Default 20. | 20 |
| duration | float | Length of the animation in seconds of playback, not of simulated time. Default 10. Together with fps this sets the number of frames: about fps * duration evenly spaced time steps are drawn, and never fewer than 2. If the simulation has no more time steps than that, every time step becomes a frame. |
10 |
| trails | bool | If True, each body leaves a wake behind it. In 2D the wake is a faint line through its most recent positions, about a tenth of the frames long. In 3D the full trajectories are drawn faintly instead. The default, False, shows only the moving markers. |
False |
| three_d | bool | None (the default) picks 3D only when there is motion in z. True forces a 3D plotly animation even for planar data. False forces a 2D animation of the projection onto the x-y plane. |
None |
| path | str or path - like | Where to save a 2D animation, for example "orbit.gif". The file is written as a GIF at fps frames per second, which needs the pillow package. The default, None, saves nothing. Ignored when the animation is 3D. |
None |
Returns
| Name | Type | Description |
|---|---|---|
| matplotlib.animation.FuncAnimation or plotly.graph_objects.Figure | A matplotlib FuncAnimation in 2D, on a new 7 by 7 inch figure. Each frame is titled with the time in days. A plotly Figure with Play and Pause buttons and a time slider in 3D. |
Raises
| Name | Type | Description |
|---|---|---|
| ValueError | If sim_data has no time column. |
See Also
animate_system_3d: The 3D animation this function switches to.plot_system: A single frozen snapshot.plot_orbits: Whole trajectories in one static plot.
Notes
To show a 2D animation in a Jupyter notebook or a Quarto document, convert it with IPython.display.HTML(anim.to_jshtml()). In a script, keep a reference to the returned object and call matplotlib.pyplot.show().
The 2D view is set once, from the full extent of sim_data with a 5 percent margin, and both axes share one scale. It does not rescale as the bodies move.
Saving a GIF draws and encodes every frame, so it takes far longer than a static plot. Keep fps * duration modest. The 3D animation involves no encoding and is built quickly.
plotly is an optional dependency. If a 3D animation is called for and plotly is not installed, a warning is issued and the 2D animation is built instead.
Examples
A short 2D animation of Earth around the Sun, shown inline: 10 frames, from 5 frames per second for 2 seconds. three_d=False states the choice of 2D outright. The figure is closed so that only the animation is displayed.
Save the animation as a GIF. A temporary directory is used here so that the example leaves nothing behind.
import os
import tempfile
import matplotlib.pyplot as plt
import pyorbiter as orb
sim = (
orb.create_system()
.add_sun()
.add_planet("Earth", parent="Sun")
.simulate(time_step=orb.seconds_per_day, duration=orb.seconds_per_year)
)
with tempfile.TemporaryDirectory() as folder:
orb.animate_system(sim, fps=5, duration=1, path=os.path.join(folder, "earth.gif"))
plt.close()
saved = os.listdir(folder)
saved['earth.gif']