Mastering Graphviz: Quick Guide to Creating Diagrams with the dot Command
This tutorial shows how to install Graphviz on Linux, write simple DOT files, generate PNG images with the dot command, and use core node, edge, and graph attributes—including subgraph clusters—to produce professional diagrams without a GUI.
Installation
sudo apt install graphvizInstalls the dot executable.
Hello World Example
Create test.dot: graph g { "Hello" -- "world" } Render PNG:
dot -Tpng -o test.png test.dotDOT Language Basics
Use graph for undirected graphs and digraph for directed graphs. Graph description is enclosed in { }. Comments: // single‑line, /* … */ block.
Node Attributes
Nodes can be defined implicitly; attributes are set in square brackets after the node name or via a default node block.
shape – geometric shape (e.g., box, ellipse, circle)
label – text displayed inside the node
style – filled or solid color – outline color
fillcolor – interior color
Example:
node [shape="box", style="filled", color="red", fillcolor="green"];Edge Attributes
Edges connect nodes; direction is determined by the graph type.
style – solid or dotted color – line color
label – text label on the edge
labelfontcolor – label font color
headlabel / taillabel – labels at the arrow head or tail
decorate – draw a decorative line for the label
For directed edges you can specify attachment points using compass points e, s, w, n.
Graph Attributes and Subgraphs
Graph‑level attributes affect layout and appearance:
size – canvas size
label – title
labelloc – position of the title ( t top, b bottom)
labeljust – alignment of the title
bgcolor – background color
rankdir – layout direction ( LR, TB, etc.)
Subgraphs (clusters) group nodes and can have their own attributes. Prefix the subgraph name with cluster to obtain a boxed grouping.
digraph graph_settings {
start [shape="Mdiamond"];
end [shape="Msquare"];
subgraph cluster_sub1 {
label="process #1";
labelloc="t";
bgcolor="gray55";
node [style="filled", color="white"];
a0 -> a1 -> a2 -> a3 -> a0;
}
subgraph cluster_sub2 {
label="process #2";
labelloc="t";
color="blue";
node [style="filled", color="black", fillcolor="gray55"];
b0 -> b1 -> b2 -> b3;
}
start -> {a0,b0};
a1 -> b3;
b2 -> a3;
{a3,b3} -> end;
}Complete Example with Node and Edge Defaults
digraph edge_settings {
edge [color="green", decorate=false]; // default edge attributes
node [shape="polygon", sides=4, color="blue"]; // default node attributes
a -> b [style="dotted", color="red", label="a to b"];
b:se -> c:w [headlabel="end", taillabel="start"]; // compass points
{c, f} -> {d, e} [label="multi-lines", decorate=true];
}Summary
The dot command provides a scriptable way to generate high‑quality diagrams. Mastering node, edge, and graph attributes covers the majority of everyday use cases; the official Graphviz documentation offers additional advanced features.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
