Skip to main content

plot_orbits

plot_orbits(sim_data, three_d=None, ax=None)

Plot the full trajectory of every body.

Draws one line per body through every recorded position. This is the quickest way to look at a simulation.

The plot is 2D or 3D depending on the data. If every z value in sim_data is zero, the motion is planar and a matplotlib plot of y against x is drawn. If any z value is non-zero, the work is handed to plot_orbits_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 and z, with positions in meters. The 3D plot also uses time, in seconds. required
three_d bool None (the default) picks 3D only when there is motion in z. True forces a 3D plotly figure even for planar data. False forces a 2D plot, which shows the projection onto the x-y plane. None
ax matplotlib.axes.Axes Axes to draw the 2D plot on. By default a new 7 by 7 inch figure is created. Ignored when the plot is 3D. None

Returns

Name Type Description
matplotlib.axes.Axes or plotly.graph_objects.Figure The matplotlib Axes for a 2D plot, with an equal aspect ratio, axes labelled in meters and a legend of body names. A plotly Figure for a 3D plot.

See Also

Notes

plotly is an optional dependency. If a 3D plot is called for and plotly is not installed, a warning is issued and the 2D plot is drawn instead.

The 2D plot returns ordinary matplotlib Axes, so it can be restyled with the usual methods (ax.set_title, ax.set_xlim and so on).

Examples

Earth and Mars around the Sun. Mars is placed in the same plane as Earth (i=0), so the motion is planar and the plot is 2D.

import pyorbiter as orb

sim = (
    orb.create_system()
    .add_sun()
    .add_planet("Earth", parent="Sun")
    .add_planet("Mars", parent="Sun", i=0)
    .simulate(time_step=orb.seconds_per_day, duration=2 * orb.seconds_per_year)
)
orb.plot_orbits(sim)

Pass ax to place the plot in a figure of your own, here next to the same data seen from Earth.

import matplotlib.pyplot as plt
import pyorbiter as orb

sim = (
    orb.create_system()
    .add_sun()
    .add_planet("Earth", parent="Sun")
    .add_planet("Mars", parent="Sun", i=0)
    .simulate(time_step=orb.seconds_per_day, duration=2 * orb.seconds_per_year)
)
fig, (left, right) = plt.subplots(1, 2, figsize=(11, 5.5))
orb.plot_orbits(sim, ax=left).set_title("Sun at rest")
orb.plot_orbits(orb.shift_reference_frame(sim, "Earth"), ax=right).set_title("Seen from Earth")