Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

GraphvizFormatting

GraphvizFormatting controls how nonlinear factor graphs are laid out when exported to Graphviz DOT. It extends DotWriter with pose-aware axes, scale, and factor-merging options.

Open In Colab
import gtsam
import numpy as np
from gtsam.symbol_shorthand import L, V, X

A graph with geometric values

The formatting object uses Values to place pose variables according to their translations. Factors are drawn between those positioned variables.

graph = gtsam.NonlinearFactorGraph()
model = gtsam.noiseModel.Diagonal.Sigmas(np.array([0.1, 0.1, 0.05]))
graph.add(gtsam.PriorFactorPose2(X(0), gtsam.Pose2(), model))
graph.add(gtsam.BetweenFactorPose2(X(0), X(1), gtsam.Pose2(2.0, 1.0, 0.2), model))

values = gtsam.Values()
values.insert(X(0), gtsam.Pose2())
values.insert(X(1), gtsam.Pose2(2.0, 1.0, 0.2))

Paper axes and scale

paperHorizontalAxis and paperVerticalAxis choose which world axes appear on the page; negative-axis enum values flip a direction. scale converts geometry units into Graphviz position units.

formatting = gtsam.GraphvizFormatting()
formatting.paperHorizontalAxis = gtsam.GraphvizFormatting.Axis.X
formatting.paperVerticalAxis = gtsam.GraphvizFormatting.Axis.Y
formatting.scale = 2.0
dot = graph.dot(values, gtsam.DefaultKeyFormatter, formatting)
print("DOT lines:", len(dot.splitlines()))
print(dot.splitlines()[0])

Factor and node styling

mergeSimilarFactors combines repeated factors with the same scope. Inherited DotWriter fields control boxes, binary edges, factor points, explicit variable/factor positions, and figure dimensions. saveGraph() writes the same DOT representation to a file.

formatting.mergeSimilarFactors = True
formatting.plotFactorPoints = True
formatting.figureWidthInches = 6.0
formatting.figureHeightInches = 4.0
merged_dot = graph.dot(values, gtsam.DefaultKeyFormatter, formatting)
assert "graph" in merged_dot

Source

GraphvizFormatting.h

AI assistance caveat

AI was used to help draft this documentation, and inaccuracies could be present.