Graphviz: An Open Source Graph Visualization Software
Graphviz (short for Graph Visualization Software) is a package of open-source tools for creating graphs. It takes text input in DOT format, generates images.
DOT Format
DOT
is a graph description language. DOT
files are usually with .gv
filename extension.
See the official document for the complete DOT
format specification. Here I use an example directed graph to show the basic concepts of the format:
// File `test.gv`:
digraph TestGraph {
// Graph (or global) attributes
bgcolor = "lightgrey";
/**
* Node name can be characters or digits
*/
a [fontcolor = "red";color = "green";];
c [shape = "triangle";];
a -> b [arrowhead = diamond; color = blue;];
b -> b;
a -> 12;
a -> 12;
a -> c -> a;
}
The DOT
file produces the following image with commands:
# Install Graphviz package if you haven't
sudo apt install graphviz
# Generate image from DOT file
dot -Tpng test.gv > test.png
Here comes some rules of the DOT
format that are reflected in the example:
- A directed graph begins with key word
digraph
. An undirected graph begins withgraph
(not for this example). - An edge connects 2 nodes (like
a->b
). Multiple connections can also be concatenated, likea->c->a
. - Node name can contain characters, digits and symbols. Digits-only name is also acceptable.
- In a directed graph, the nodes are connected with
->
. While in the undirected graph, the edges should be--
. - Attributes are represented with key-value pairs.
- Graph (or global) level attributes are written in the body of the graph. Node attributes are written after a stand-alone node definition. Edge attributes are written after the connection statement.
- Comments can be either
//
or/* ... */
Layout Engine
The DOT
format only describe the relation of nodes, edges and their attributes, but doesn’t determine how the nodes and edges are placed (the layout) in a picture.
When you generate the image from a DOT
file, there are different engines to choose:
dot
: Hierarchical or layered drawings of directed graphsneato
: “Spring model” layoutsfdp
: Force-Directed Placementsfdp
: Scalable Force-Directed Placementcirco
: Circular layouttwopi
: Radial layoutnop
: Pretty-print DOT graph file. Equivalent to nop1nop2
: Pretty-print DOT graph file, assuming positions already knownosage
: Draws clustered graphspatchwork
: Draws map of clustered graph using a squarified treemap layout
After installing the Graphviz
software package, each engine is available as an installed binary. The filename is same with the name of the engine.
The following diagram shows the work of different engines on the example DOT file.
Tools
In my VS Code editor, I installed an extension called Graphviz Interactive Preview
, which is quite convenient.
Here is a screenshot of the tool when I was creating the example DOT file. The tool provides some popup menus for the ease of editing. And the preview updates in real-time.