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)
)
orb.plot_orbits(sim);
pyorbiter is the Python sibling of the R package orbitr. If you already know orbitr, you already know most of pyorbiter. Function names, argument names, constants, and defaults are the same wherever the two languages allow it. This page lists what changes.
In R:
In Python:

The differences you can see here are the differences you will meet everywhere:
library(orbitr) puts every name in scope. In Python you write import pyorbiter as orb and prefix names with orb., including the constants (orb.seconds_per_day).|> becomes a dot. Building a system is a chain of method calls..simulate(...), a short spelling of orb.simulate_system(system, ...).TRUE, FALSE, and NULL become True, False, and None, and c("Pluto", "Moon") becomes the list ["Pluto", "Moon"].The R column shows each call in its plain form. In practice you would write most of them with the pipe.
| orbitr (R) | pyorbiter (Python) |
|---|---|
create_system() |
orb.create_system() |
add_body(system, ...) |
system.add_body(...) or orb.add_body(system, ...) |
add_sun(system) |
system.add_sun() or orb.add_sun(system) |
add_body_keplerian(system, ...) |
system.add_body_keplerian(...) or orb.add_body_keplerian(system, ...) |
add_planet(system, "Earth", parent = "Sun") |
system.add_planet("Earth", parent="Sun") or orb.add_planet(system, ...) |
remove_body(system, c("Pluto", "Moon")) |
system.remove_body(["Pluto", "Moon"]) or orb.remove_body(system, ...) |
load_solar_system() |
orb.load_solar_system() |
get_bodies(system) |
system.bodies, system.get_bodies(), or orb.get_bodies(system) |
print(system) |
print(system), or just system at the prompt |
simulate_system(system, ...) |
system.simulate(...) or orb.simulate_system(system, ...) |
continue_simulation(sim, ...) |
orb.continue_simulation(sim, ...) |
system_from_simulation(sim) |
orb.system_from_simulation(sim) |
shift_reference_frame(sim, "Earth") |
orb.shift_reference_frame(sim, "Earth") or sim.pipe(orb.shift_reference_frame, "Earth") |
get_orbital_elements(sim, "Moon", "Earth") |
orb.get_orbital_elements(sim, "Moon", "Earth") |
get_energy(sim), get_momentum(sim), get_angular_momentum(sim) |
orb.get_energy(sim), orb.get_momentum(sim), orb.get_angular_momentum(sim) |
conserved_quantities(sim) |
orb.conserved_quantities(sim) |
plot_orbits(), plot_system() (ggplot2) |
orb.plot_orbits(), orb.plot_system() (matplotlib) |
plot_orbits_3d(), plot_system_3d() (plotly) |
orb.plot_orbits_3d(), orb.plot_system_3d() (plotly) |
animate_system() (gganimate) |
orb.animate_system() (matplotlib) |
animate_system_3d() (plotly) |
orb.animate_system_3d() (plotly) |
save_system() writes .rds |
orb.save_system() or system.save() writes JSON |
load_system() |
orb.load_system() |
export_bodies() writes CSV |
orb.export_bodies() or system.export_bodies() writes CSV |
mass_earth, distance_earth_sun, seconds_per_day, … |
orb.mass_earth, orb.distance_earth_sun, orb.seconds_per_day, … |
| a tibble | a pandas DataFrame, or a polars DataFrame |
| attributes on the tibble | sim.attrs (pandas) or orb.run_settings(sim) (either) |
the C++ kernel, use_cpp |
a vectorized numpy kernel, no switch |
Three things exist only on the Python side:
orb.set_backend(), orb.get_backend(), and the backend= argument choose between pandas and polars output. See pandas or polars.orb.run_settings() reads the settings of a run from either kind of DataFrame.orb.calc_acceleration() exposes the acceleration kernel.In R a system is an orbit_system object that you pass from one function to the next with |>. In Python a system is an OrbitSystem object, and each builder is a method that returns a new system:
───────────────────────────────── orbit_system ─────────────────────────────────
G: 6.6743e-11 (standard)
Bodies: 3
id mass x y z vx vy vz
Sun 1.989000e+30 0.000000e+00 0.000000e+00 0.000000e+00 0.000000 0.000000 0.000000
Earth 5.972000e+24 -3.296559e+10 1.433603e+11 0.000000e+00 -29520.187051 -6788.143346 0.000000
Mars 6.417000e+23 1.887900e+11 -8.370696e+10 -6.395443e+09 10749.771467 24226.116460 243.021783
As in R, nothing is modified in place. system.remove_body("Mars") returns a new system and system still has three bodies:
Every method also has a function form that takes the system as its first argument, with the same name as in R. The two forms give the same system:
True
One small difference in the signatures: parent must be given by name in add_planet() and add_body_keplerian().
The analysis and plotting functions take a simulation DataFrame, not a system, so they are plain functions. Nest them, or use the DataFrame’s own pipe method to read left to right as you would in R:
attrs and run_settings()orbitr records the settings of a run (G, softening, method, time_step) as attributes on the tibble that simulate_system() returns. pyorbiter records the same four settings. With pandas they are in the DataFrame’s attrs dictionary:
orb.run_settings() returns the same thing and also works for polars DataFrames, which have no attrs:
{'G': 6.6743e-11, 'softening': 0, 'method': 'verlet', 'time_step': 86400}
As in R, the functions that need these settings, such as get_energy(), conserved_quantities(), and continue_simulation(), pick them up by default. There is one catch with polars: the settings are remembered only for the exact DataFrame pyorbiter returned. A frame you derive from it has lost them:
If such a run did not use the default G, softening, or method, pass them explicitly. pandas or polars has the details.
The simulation has the same columns as in R: id, mass, x, y, z, vx, vy, vz, time, with one row per body per time step.
| id | mass | x | y | z | vx | vy | vz | time | |
|---|---|---|---|---|---|---|---|---|---|
| 0 | Earth | 5.972000e+24 | 0.000000e+00 | 0.0 | 0.0 | 0.000000 | 0.000000 | 0.0 | 0.0 |
| 1 | Moon | 7.342000e+22 | 3.844000e+08 | 0.0 | 0.0 | 0.000000 | 1022.000000 | 0.0 | 0.0 |
| 2 | Earth | 5.972000e+24 | 2.148960e+02 | 0.0 | 0.0 | 0.119384 | 0.000571 | 0.0 | 3600.0 |
| 3 | Moon | 7.342000e+22 | 3.843825e+08 | 3679200.0 | 0.0 | -9.710720 | 1021.953527 | 0.0 | 3600.0 |
Where you would reach for dplyr in R, use pandas (or polars) directly:
| dplyr | pandas |
|---|---|
filter(sim, id == "Moon") |
sim[sim["id"] == "Moon"] |
mutate(sim, r = sqrt(x^2 + y^2)) |
sim.assign(r=(sim["x"]**2 + sim["y"]**2) ** 0.5) |
summarise(group_by(sim, id), max_x = max(x)) |
sim.groupby("id")["x"].max() |
Chained together, with a change of reference frame first, these give the smallest and largest distance of each body from Earth:
In R, the 2D plotters return a ggplot object and you add layers with +. In Python they return a matplotlib Axes, and you customize by calling its methods:
sim = (
orb.create_system()
.add_sun()
.add_body("Earth", mass=orb.mass_earth, x=orb.distance_earth_sun, vy=orb.speed_earth)
.simulate(time_step=orb.seconds_per_day, duration=orb.seconds_per_year)
)
ax = orb.plot_orbits(sim)
ax.plot(0, 0, "o", markersize=12, color="C0")
ax.set_title("Earth-Sun Orbit");
To place the plot inside a figure of your own, for example next to another panel, pass an existing Axes with ax=:

The rest of the plotting behavior carries over:
z gives an interactive plotly figure, and three_d=True or three_d=False forces one or the other.animate_system() uses matplotlib in place of gganimate. It returns a FuncAnimation, and path="orbit.gif" saves it as a GIF.See Custom Visualization and 3D Plotting.
save_system() in R writes an .rds file, which only R can read. In Python it writes JSON. The two files are not interchangeable, so to move a system between the languages, use export_bodies(). It writes the body table as a CSV in both packages, with the same columns.
Yes. pyorbiter implements the same equations with the same constants, and its test suite compares its output with numbers produced by orbitr 1.0.0. The reference numbers are stored in tests/reference/r_reference.json, the R script that produced them is tests/reference/make_reference.R, and the comparisons are in tests/test_parity_with_r.py.
Here is one of those comparisons done by hand: Earth after 365 one-day steps around the Sun. The R values are copied from the reference file.
import pandas as pd
sim = (
orb.create_system()
.add_body("Sun", mass=orb.mass_sun)
.add_body("Earth", mass=orb.mass_earth, x=orb.distance_earth_sun, vy=orb.speed_earth)
.simulate(time_step=orb.seconds_per_day, duration=365 * orb.seconds_per_day)
)
python = sim[sim["id"] == "Earth"].iloc[-1][["x", "y", "vx", "vy"]].astype(float)
r = pd.Series({"x": 149599848652.46, "y": 215519178.446102,
"vx": -42.3704376409083, "vy": 29779.9698859056})
pd.DataFrame({"python": python, "R": r, "relative difference": (python - r).abs() / r.abs()})| python | R | relative difference | |
|---|---|---|---|
| x | 1.495998e+11 | 1.495998e+11 | 6.119841e-16 |
| y | 2.155192e+08 | 2.155192e+08 | 2.815274e-12 |
| vx | -4.237044e+01 | -4.237044e+01 | 2.513286e-12 |
| vy | 2.977997e+04 | 2.977997e+04 | 2.443239e-15 |
The tests assert agreement to these tolerances:
| What is compared | Relative tolerance |
|---|---|
Initial states from load_solar_system() and from a hyperbolic add_body_keplerian() |
\(10^{-12}\) |
| Final positions and velocities for all three integrators, a continued run, a 30-day solar system run, a softened 3D three-body run, and the barycentric and Earth-centered frames | \(10^{-8}\) |
Energy, momentum, and angular momentum from conserved_quantities() and get_energy() |
\(10^{-8}\) |
Orbital elements from get_orbital_elements() (Moon, Mars, and a hyperbolic flyby) |
\(10^{-7}\) |
For positions and velocities the tolerance is relative to the largest value in each column, so a quantity that is nearly zero, such as the Sun’s small wobble, is compared on the scale of the planets’ values. The relative error columns of conserved_quantities() are differences of nearly equal numbers, so the tests check them only loosely: to one part in a thousand, or to within \(10^{-14}\).
In short, a simulation built the same way in both packages gives the same numbers to about eight significant figures.