A Comprehensive Guide to Graphviz Layout Options: Tips and Tricks for Better Graphs

DevTools Daily
3 min readFeb 16, 2023

--

This article is for people who are already familiar with GraphViz and are looking at ways to make their graphs look prettier.

Here we’ll look into several less known but very important layout options. We are skipping well known LR/TD directions, and clusters.

That would be our starting point, let’s say we are creating a simple system architecture diagram.

digraph Graph {
node[shape=rect]
gateway -> users
gateway -> companies
gateway -> groups
}

open in playground

specify Width and Heigh of nodes

explicitly set width and height of nodes

digraph Graph {
node[shape=rect]
gateway[width=3 height=0.3]
gateway -> users
gateway -> companies
gateway -> groups
}

open in playground

splines — change arrow line shapes

digraph Graph {
node[shape=rect]
splines=ortho
gateway[width=3 height=0.3]
gateway -> users
gateway -> companies
gateway -> groups
}

open in playground

ranksep — change spacing between ranks

digraph Graph {
node[shape=rect]
splines=ortho
gateway[width=3 height=0.3]
gateway -> users
gateway -> companies
gateway -> groups
ranksep=1.5
}

open in playground

nodesep — change spacing between nodes

digraph Graph {
node[shape=rect]
splines=ortho
gateway[width=3 height=0.3]
gateway -> users
gateway -> companies
gateway -> groups
nodesep=1
}

open in playground

Now let’s say I add one more edge, and it changes the weights of nodes and makes graph looks like that

digraph Graph {
node[shape=rect]
splines=ortho
gateway[width=3 height=0.3]
gateway -> users
users -> companies
gateway -> companies
gateway -> groups
nodesep=1
}

there are 2 ways to address that

apply rank to subgraph items

you can apply same ‘rank’ to a subgraph

digraph Graph {
node[shape=rect]
splines=ortho
gateway[width=3 height=0.3]
gateway -> users
users -> companies
gateway -> companies
gateway -> groups
nodesep=1
{
rank=same
users
companies
}
}

open playground

constraint

if you see that some arrow adds to much weight to a node, you can exclude it from ranking

digraph Graph {
node[shape=rect]
splines=ortho
gateway[width=3 height=0.3]
gateway -> users
users -> companies[constraint=false]
gateway -> companies
gateway -> groups
nodesep=1
}

open playground

Please checkout some of our useful resources:

  1. Graphviz cheatsheet
  2. Graphviz Playground
  3. Graphviz interactive tutorial
  4. Graphviz vs MermaidJS comparison

This article was originally published in DevToolsDaily blog

--

--

No responses yet