Cheatsheet: Graphviz

Minimal complete examples

Small valid directed graph

digraph G {
  rankdir=LR
  client -> api -> db
}

Small valid undirected graph

graph G {
  A -- B
  B -- C
}

Nodes

Node in default style

client
server
Node in default style

Node stroke color

client[color=red]
server[color=blue]
Node stroke color

Node filled color

client[style=filled fillcolor=yellow]
Node filled color

Node label

client[label="Remote client"]

Node shape

client[shape="circle"]

Node size

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

Edges

Edge in default style

client -> server

Edge label

client -> server[label="send request"]

Edge styles

client -> server[style=dashed]
client -> cache[style=dotted]

Edge color

client -> server[color=green]

Layout and ranking

Direction and spacing controls

digraph G {
  rankdir=LR
  ranksep=1.0
  nodesep=0.5
  A -> B -> C
}

Keep nodes on the same rank

digraph G {
  { rank=same; api; auth; billing; }
  client -> api
  client -> auth
  client -> billing
}

Change layout engine (CLI)

dot -Kdot graph.dot -Tsvg -o graph.svg
dot -Kneato graph.dot -Tpng -o graph.png

Subgraphs and clusters

Subgraph

{
  node [color=red]
  javascript -> api
}

subgraph server {
  edge [style=dashed]
  api -> backend
}
Subgraph

A subgraph named cluster* is rendered as a cluster

subgraph cluster_0 {
  style=filled;
  color=lightgrey;
  javascript -> api;
  label="frontend";
}
A subgraph named cluster* is rendered as a cluster

Defaults

Set default style for all nodes

node[style=filled];

Set default style for all edges

edge[style=dotted];

Common pitfalls

Quote node IDs with spaces

"api gateway" -> "user service"
"api gateway"[label="API Gateway"]

Clusters need prefix cluster_

subgraph cluster_backend {
  label="Backend"
  api -> db
}

Use strict mode to remove duplicate edges

strict digraph G {
  A -> B
  A -> B
}

See also: