Graphviz
Basic Structure
// Filename: graph.dot
digraph G {
// Nodes
A [label="Node A"];
B [label="Node B"];
// Edges
A -> B [label="Edge from A to B"];
}
Common Attributes
node [shape=shape_name, color=color, style=style, label=label];
edge [color=color, style=style, label=label];
Node Shapes
Edge Styles
Graph Types
Subgraphs
Attributes and Styles
node_name [attribute=value, ...];
edge_name [attribute=value, ...];
graph_name [attribute=value, ...];
Labels
Rankdir (Layout Direction)
rankdir=TB // Top to bottom (default)
rankdir=LR // Left to right
rankdir=BT // Bottom to top
rankdir=RL // Right to left
Group and Rank
{rank=same; node1; node2; ...;} // Horizontal alignment
{rank=min; node1; node2; ...;} // Align nodes to the top
{rank=max; node1; node2; ...;} // Align nodes to the bottom
Clusters
Node and Edge Attributes
node_name [shape=shape_name, color=color, style=style, label=label];
edge_name [color=color, style=style, label=label];
Colors
Running Graphviz
Example Output Formats
png
svg
pdf
ps
jpeg
Command Line Options
-G
: Set default graph attributes-N
: Set default node attributes-E
: Set default edge attributes-K
: Specify layout engine (dot
,neato
,twopi
,circo
,fdp
)-o
: Output file
Basic DOT Syntax:
// Create a directed graph
digraph G {
// Nodes
A [label="Node A"];
B [shape=box, label="Node B"];
// Edges
A -> B [label="Edge from A to B"];
}
Node Attributes:
shape
: Node shape (e.g., box, circle, ellipse, etc.)color
: Node colorstyle
: Node style (e.g., filled, dashed, bold, etc.)label
: Node labelfontsize
: Font size for the labelfontcolor
: Font color for the labeltooltip
: Tooltip text
Edge Attributes:
label
: Edge labelcolor
: Edge colorstyle
: Edge style (e.g., dashed, dotted, solid, etc.)dir
: Edge direction (e.g., forward, back, both, none)weight
: Edge weight for layout algorithms
Graph Attributes:
rankdir
: Layout direction (e.g., TB, LR)bgcolor
: Background color of the graphlabel
: Graph labelfontsize
: Font size for the labelfontcolor
: Font color for the label
Examples:
Directed Graph with Subgraphs:
digraph G {
// Node and edge definitions
subgraph cluster_0 {
label="Subgraph 1";
// Nodes and edges within subgraph
}
subgraph cluster_1 {
label="Subgraph 2";
// Nodes and edges within subgraph
}
A -> B;
B -> C;
{ rank=same; A, B, C }
}
Undirected Graph with Attributes:
graph G {
// Node attributes
A [shape=box, color=blue];
B [shape=circle, color=red];
// Edge attributes
A -- B [label="Edge between A and B", color=green];
// Graph attributes
rankdir=LR;
label="My Undirected Graph";
}
Rendering DOT Files:
- Save your DOT code in a
.dot
file. - Use the
dot
command to render the graph in various formats:
Layout Algorithms (used with -K
flag):
dot
: Hierarchical layout (default for directed graphs)neato
: Spring model layout (default for undirected graphs)fdp
: Force-directed placementsfdp
: Multiscale version of fdpcirco
: Circular layouttwopi
: Radial layout
Graphviz Resources:
Remember that this cheat sheet provides a basic overview of Graphviz and the DOT language. For more complex diagrams and advanced features, refer to the official documentation and examples provided by the Graphviz community.