First Steps with GraphViz

This year I moved my whole presentations toolchain over to LaTeX and PDF, and where I added diagrams (which doesn’t happen a lot!) I used graphviz to generate them. Graphviz is a way of describing items and their relationships in a textual manner, and rendering them into a graph – and the results can be saved as an image, or included in LaTeX documents.

I found graphviz frustrating at times but on the whole it draws clean, symmetrical graphs far beyond anything else I can manage, even if I do use a mouse or tablet. Since I’m not able to use a pointing device on a regular basis, and I’m marking up my presentations in text also, it turned out to be a really good fit. I thought I’d share how I got on with it and some of my own graphs – as much to remind me next time conference season comes around as anything.

Dependencies

I’m on Ubuntu, so there’s a graphviz package which I installed. There’s a couple of different graphviz dialects but I found the dot language was approachable – see the wikipedia page for a quick overview.

Generating Diagrams

The command I use for generating diagrams from the command line looks something like this:

dot step1.dot -Tps -o step1.ps

You can output to all sorts of formats, such as PNG. The PostScript format plays well with my LaTeX content.

Example Diagrams

To give you an idea of the results, I thought I’d pull a couple of diagrams from a talk I gave recently, called “Understanding Distributed Source Control”. The two graphs show the difference between a centralised pattern, and the ability to move changes between any two repos. I’ll show both the code and the images:

graph 1

graph distributed {

graph [nodesep=0.5]
node [fontname="Helvetica", color="Lavender", style="filled"]
edge [color="Gray"]

repo [label="repo"]
wc1 [label="repo"]
wc2 [label="repo"]
wc3 [label="repo"]
wc4 [label="repo"]

repo -- wc1
repo -- wc2
repo -- wc3
repo -- wc4
}

graph 2

graph distributed {

graph [nodesep=0.5]
node [fontname="Helvetica", color="Lavender", style="filled"]
edge [color="Gray"]

repo [label="repo"]
wc1 [label="repo"]
wc2 [label="repo"]
wc3 [label="repo"]
wc4 [label="repo"]

repo -- wc1
repo -- wc2
repo -- wc3
repo -- wc4
wc1 -- wc2
wc1 -- wc3
wc1 -- wc4
wc2 -- wc3
wc2 -- wc4
wc3 -- wc4
}

As you can see, there’s no control over placement in these examples. I’ve simply added nodes and connected them, and the rest is done automagically. I quite like the effect of what happens when you link all the distributed nodes together, it looks a bit chaotic – but working with DVCSes can be a little bit like that :)

I’m using this approach for diagrams both for slides and for the book I’m working on, so I intend to blog a few more examples of how I’m using graphviz.

One thought on “First Steps with GraphViz

  1. Thanks for the basic and clear examples. I am merely a retired maths prof, who only knows how to program in Octave/Matlab.==> herz-fischler.ca/matlab
    However I am doing the genealogy and history of our ancestors and I heard about graphviz from the maths genealogy project. So today I started learning graphviz. Yours examples were the clearest. So thanks

    Roger H-F

Leave a Reply

Please use [code] and [/code] around any source code you wish to share.

This site uses Akismet to reduce spam. Learn how your comment data is processed.