Graphviz is the graph wiz

Updated: September 1, 2014

Creating beautiful, compelling and all-telling graphs is not a simple thing. If you think you can just punch in a few numbers and expect charts to become a Kubrick kind of work all on their own, well they ain't. And to say nothing of more complex shapes and forms, or dependency graphs.

Graphviz is one of those tools that can help you bridge the artistic and talent gap present in your soul and fingers, maybe. Quoting the official page, Graphviz is open-source graph visualization software. Graph visualization is a way of representing structural information as diagrams of abstract graphs and networks. It has important applications in networking, bioinformatics, software engineering, database and web design, machine learning, and in visual interfaces for other technical domains. Jolly. Exactly what we need.


Graphviz tour & guide

Working with this software may appear a little less intuitive for most people. It is because you input graph data using text, and then convert the data into a graph. Sort of like XML. But if you've followed my gnuplot article a few days back, you will be all right. 

In Linux, grab the program from the repo and start writing down the relations for your objects. In Windows, you even get a neat little GUI, with two views, one for your actual data and one for your representation.

GUI on Windows

Graphviz files usually bear .gv extensions, although this is not a must. The files contain directives that explain relations between objects in the graph, sort of like what we did with systemd blame, if you recall, plus a certain amount of styling that can help you make the visualization prettier and more compelling.

While it may appear innocent at first, there's a huge amount of smart in this little program. Tons of options, both documented and undocumented, clever algorithms, and quite a bit of fancy features, which you must expose through the command line. Not simple but very doable. Let's take a look at a few sample commands from my distro work:

digraph "linux" {
    node [fontname = "Sans"];
    graph [overlap = false, margin=3.0];

Here, we define a directed graph section, which means there ought to be little arrows between graph objects, which is quite useful for flowcharts and dependency graphs. Next, we define splines. Then, for DOT graphs, we have the minimum space between two adjacent nodes in the same rank, in inches, as defined by nodesep. Similarly, different ranks in the graph are separated by at least 0.5 inches of space. The same goes for lines. Margins are pretty self-explanatory, including the overall graph margin, and overlap beyond its borders. Finally, we use the Sans font for all node labels.

You might also be interested in defining the default graph properties. This includes the overall size of the canvas, as well as the font attributes and the title, or label. For each node, you can specify the shape, the number of sides, a possible 2D placement offset, style, and still other options. You will need to invest some time to read the help file.

graph [fontname = "Sans",
       fontsize = 48,
       label = "Linux is very easy,",
       size = "30,38"];
node  [shape = polygon,
       sides = 4,
       distortion = "0.0",
       orientation = "0.0",
       skew = "0.0",
       color = white,
       style = filled,
       fontname = "Sans" ];

And here, we have a subgraph, which defines the dependencies between several nodes, but in a clever way. In order to have them all belong to the same rank, we create the necessary lines, but make them invisible and unconstrained. Besides that, there's the usual background color, label font type, size, position, and the string. The extra empty lines in the title are just to make it all a little tidier in the final output.

subgraph "cluster" {
  node  [shape=circle,style=filled];
  graph [color=white,label="\nPackage managers\n\n",
         labelloc=b,fontsize=24,fontname = "Sans"];
  "DEB" -> "RPM" -> "PET" [style=invis, constraint=false];

Some node attributes and dependencies:

"Mint" [sides=1 color=green];
"Debian" [sides=1 color=thistle3];

"Ubuntu" -> "Unity" [color=orange];
"Fedora" -> "KDE" [color=dodgerblue2];
"Fedora" -> "Cinnamon" [color=dodgerblue2];
"Fedora" -> "MATE" [color=dodgerblue2];

And finally you can create some nice graphs. Indeed, I used Graphviz to render my nice Illustrated Linux Distro Newbie Guide, which just shows how complex the world of Linux is, and how it may appear to a newcomer, with all its funny distro names and forks, graphical desktop environments, package managers, and such.

Linux distro dependency graph

Anyhow, there are many types of views you can use, for the same data. Circular or neatly compacted graphs, anything you fancy. Once again, we did a similar thing with yED, so there you go. Lovely jubbly.

Dot graph window

Neato graph Circo graph


Graphviz is not the easiest or most intuitive software in the world, but it is mighty powerful, and will empower you with a fresh dose of geek joy, in addition to providing you with beautiful visual art. After you get the hang of how it works, in that you will be seeing your product only after providing command inputs, sort of like LaTeX, you will find this mode of work extremely productive and distraction free.

Anyhow, it's zero cost, so there's nothing to it. Download the program and start playing. You may discover that the hundreds of hours you spent aligning text boxes in Powerpoint may have been a futile exercise after all. There's a superior and better looking method of making graphs, and you've just learned about it. I hope you liked this.


You may also like: