How to draw multiple graphs with dot?

knarf picture knarf · Mar 17, 2011 · Viewed 7.5k times · Source

I have a print_dot() function that outputs dot on stdout. That way I can do:

$ ./myprogram < input | dot -T x11

It works great when I try to print one graph.

Now when I print several graphs, nothing shows up. The dot window is blank, X11 and dot take all the CPU. Nothing is printed on stderr.

$ echo -e "graph  { a -- b }" | dot -T x11 # work
$ echo -e "graph  { a -- b } \n graph { c --d }" | dot -T x11 # doesn't work

# it seems to be interpreted nonetheless
$ echo -e "graph  { a -- b } \n graph { c -- d } " | dot -T xdot
graph {
    ...
}
graph {
    ...
}

Also, when I remove the \n between the 2 graphs, only the first graph is interpreted (what a nice feature...):

$ echo -e "graph  { a -- b }  graph { c -- d } " | dot -T xdot
graph {
    ...
}

Piping the xdot output to dot again doesn't fix the problem.

So, how does one render multiple graphs with graphviz?

Answer

Gareth McCaughan picture Gareth McCaughan · Mar 17, 2011

One calls dot multiple times. Or one puts everything into a single graph, taking care to avoid duplication of names.