Small, readable examples of graph-based SLAM with GTSAM. The examples are Python reimplementations of toy problems from g2o_tutorials.
The tutorials use synthetic data so the factors, noise models, and optimization steps stay easy to follow.
Pose-graph SLAM — pgSlam/pgSlam.ipynb
A robot follows an oval path. Noisy wheel odometry creates drift; loop closures correct it.
- Generate a 120-pose ground-truth trajectory.
- Add noise to relative odometry, then integrate it to show drift.
- Build a
NonlinearFactorGraphwith a prior, 119 odometry factors, and 20 loop-closure factors. - Optimize with GTSAM's Levenberg–Marquardt optimizer and inspect marginal covariances.
- The reusable helpers are in
pgSlam/pgSlam.py.
Landmark SLAM — landmarkSlam/landmarkSlam.ipynb
A robot observes the eight corners of a cube from five poses.
- Generate local landmark observations in each robot frame.
- Estimate relative motion with point-to-point ICP.
- Build a g2o text graph with
VERTEX_SE3:QUATrobot poses,VERTEX_TRACKXYZlandmarks, andEDGE_SE3_TRACKXYZobservations. - Optimize robot poses and cube landmarks together with GTSAM.
- The reusable helpers are in
landmarkSlam/landmarkSlam.py.
Gtsam_examples/Pose2Slam.ipynb is a small 2D pose graph with a loop closure. Gtsam_examples/Landmark2DSlam.ipynb demonstrates pose and landmark optimization with BearingRangeFactor2D.
The examples are tested with GTSAM 4.3.0 and Python 3.11. A ready-to-use Conda environment is included:
conda env create -f environment.yml
conda activate gtsam-tutOr install the Python dependencies into an existing Python 3.11+ environment:
python -m venv .venv
source .venv/bin/activate
python -m pip install -r requirements.txtThe landmark tutorial uses Open3D for optional 3D visualization. The optimization and all 2D examples run without a display.
Start Jupyter from the repository root:
conda activate gtsam-tut
jupyter labThe landmark helper can also be run as a headless script:
python landmarkSlam/landmarkSlam.pyUse --show to enable the optional Open3D windows when a display is available. Generated .g2o files are written to the current working directory and are ignored by Git.
pgSlam/ pose-graph notebook, helper module, and reference figures
landmarkSlam/ landmark notebook, helper module, and reference figures
Gtsam_examples/ minimal GTSAM notebooks
Media/ additional screenshots
The Notion notes contain the original longer discussion of the examples.
This repository does not currently include a separate license file. Check the repository history and upstream tutorial before reusing the examples.

