Python for robotics: the shortlist
NumPy, SciPy, PyTorch/JAX, matplotlib — four libraries cover 90% of robotics Python. Here's the minimum fluency and the idioms that actually show up in robot code.
Python is the default language of modern robotics. You don't need to be a Python expert to build robots — you need working fluency in four libraries. Here's the exact shortlist and the idioms you'll use every day.
The four
- NumPy — arrays, vectors, matrices, math operators
- SciPy — optimization, linear algebra helpers, signal processing
- PyTorch or JAX — neural networks, autograd
- Matplotlib — plots for debugging and papers
Everything else — Pydantic, FastAPI, requests — is fine to reach for when you need it. These four you need cold.
NumPy: the one library you must know
A robot's entire state is a NumPy array. Every sensor reading, every pose, every trajectory lives in np.ndarray.
import numpy as np
# A 3D position
p = np.array([1.0, 2.0, 3.0])
# A 2D rotation by theta
theta = np.pi / 4
R = np.array([
[np.cos(theta), -np.sin(theta)],
[np.sin(theta), np.cos(theta)],
])
# Matrix-vector multiply
v = np.array([1.0, 0.0])
print(R @ v) # (0.707, 0.707)
# Element-wise vs matrix math
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
a * b # element-wise: [4, 10, 18]
a @ b # dot product: 32
np.cross(a, b) # cross product
The three operators to know cold: * (element-wise), @ (matrix multiply / dot product), np.cross. If you confuse * and @ for matrices, your robot will calmly drive into a wall.
NumPy idioms that show up everywhere
# Broadcasting: apply a scalar or row to every element
points = np.array([[1, 0], [0, 1], [1, 1]]) # 3 points
centered = points - points.mean(axis=0) # subtract centroid
# Vectorized trig on arrays
angles = np.linspace(0, 2*np.pi, 100)
xs = np.cos(angles)
ys = np.sin(angles)
# Slicing — last column of a trajectory
trajectory = np.zeros((100, 3)) # 100 timesteps × (x, y, theta)
headings = trajectory[:, 2]
last_pose = trajectory[-1]
# Stack arrays along a new axis
combined = np.stack([xs, ys], axis=1) # shape (100, 2)
SciPy: when you need the heavier tools
SciPy is NumPy's older sibling. You'll pull three modules regularly:
scipy.optimize— minimize a cost function, solve nonlinear systems (used for iterative IK, calibration)scipy.spatial.transform.Rotation— convert between quaternions, rotation matrices, Euler angles without writing the math yourselfscipy.signal— filtering, convolution, resampling for sensor data
from scipy.spatial.transform import Rotation as R
# Quaternion (x, y, z, w) → rotation matrix
q = [0, 0, 0.707, 0.707] # 90° around z
M = R.from_quat(q).as_matrix()
# Euler (roll, pitch, yaw) → quaternion
r = R.from_euler('xyz', [0, 0, 1.57])
print(r.as_quat())
Write your own quaternion math exactly once to learn it. Then never again — use scipy.spatial.transform.Rotation for the rest of your career.
PyTorch vs JAX
For any neural-net work (RL, imitation learning, perception models), pick one and stick with it.
- PyTorch: the default. Bigger ecosystem, better debugging, most tutorials use it. Eager-mode by default;
torch.compileadds jit. Recommended for most learners. - JAX: functional, composable, extremely fast when batched on GPU/TPU. Underlies Brax, MJX, and many modern RL libraries. Painful if you want to mutate a tensor mid-loop.
In 2026: default PyTorch, switch to JAX if your project specifically uses MJX or Brax.
Matplotlib: not optional
Every robotics paper has plots. Every debugging session has plots. Every third bug you fix starts with "I plotted the data and noticed…"
import matplotlib.pyplot as plt
plt.plot(time, velocity, label='commanded')
plt.plot(time, measured_velocity, label='measured')
plt.xlabel('time (s)')
plt.ylabel('velocity (m/s)')
plt.legend()
plt.title('Velocity tracking')
plt.show()
Fancier options (Plotly, seaborn) are nice. Matplotlib is required — every ROS bag analysis script you'll ever see uses it.
The Python features that bite roboticists
- GIL. One Python thread runs at a time. For CPU-bound parallelism, use
multiprocessing. For I/O parallelism (network, disk), threads or async are fine. - Garbage collection pauses. CPython's GC can introduce millisecond pauses. Don't run safety-critical real-time loops in Python. Use C++ for the 1 kHz controller, Python for the 20 Hz planner.
- Environment hell.
pip,conda,uv,poetry,rye— pick one. In 2026,uvis the fastest and works with all the others. Put every project in a venv.
A 50-line script that uses all four
import numpy as np
from scipy.optimize import minimize
import matplotlib.pyplot as plt
# Fit a circle to noisy 2D points
true_center, true_radius = np.array([1.0, 2.0]), 3.0
thetas = np.linspace(0, 2*np.pi, 100)
points = (true_center +
true_radius * np.stack([np.cos(thetas), np.sin(thetas)], axis=1) +
0.1 * np.random.randn(100, 2))
def loss(params):
cx, cy, r = params
distances = np.linalg.norm(points - [cx, cy], axis=1)
return np.mean((distances - r)**2)
result = minimize(loss, [0, 0, 1], method='Nelder-Mead')
cx, cy, r = result.x
fig, ax = plt.subplots()
ax.scatter(points[:, 0], points[:, 1], s=8)
circle = plt.Circle((cx, cy), r, fill=False, color='red')
ax.add_patch(circle)
ax.set_aspect('equal')
plt.show()
print(f'fit center=({cx:.2f}, {cy:.2f}), radius={r:.2f}')
NumPy for arrays, SciPy for optimization, Matplotlib to visualize. This is what 60% of robotics analysis scripts look like. Master this pattern and you're productive.
Comments
Sign in to post a comment.