Multiple graphs inside Graphviz DOT file

Viewed 2542

I have this Graphviz DOT graph:

digraph unit_test {
    label="Unit test"
    
    edge [fillcolor="#a6cee3" color="#1f78b4"]

    node[shape="ellipse" style="filled" fillcolor="#1f77b4"]
        start
        end
    node[shape="box" style="filled" fillcolor="#ff7f0e"]
        process
        
    subgraph cluster_process {
        label = "Major logic"
        process
    }
    
    start -> process
    process -> end
    
}

The above renders as:

First graph

I have this second graph:

digraph details {
    label = "Process details"
    
    edge [fillcolor="#a6cee3" color="#1f78b4"]
    
    node[shape="ellipse" style="filled" fillcolor="#1f77b4"]
        start
        end
    node[shape="box" style="filled" fillcolor="#ff7f0e"]
        details
    
    subgraph cluster_details {
        label = "Details"
        details
    }
    
    start -> details
    details -> end
}

Which renders to:

Second graph

Problem

When I put the above two graphs inside the same DOT file named supporting.dot and I run dot -Tpng -o supporting.png supporting.dot command, terminal prints out some jiberish and the output image file won't contain both graphs, it just contains the first one. Is it possible to use multiple graphs inside a single DOT file? If so, what am I missing?

Terminal output

3 Answers

Question is unclear about what is to be accomplished, but maybe the following is a starting point

digraph G{
subgraph unit_test {
    label="Unit test"
    
    edge [fillcolor="#a6cee3" color="#1f78b4"]

    node[shape="ellipse" style="filled" fillcolor="#1f77b4"]
        start
        end
    node[shape="box" style="filled" fillcolor="#ff7f0e"]
        process
        
    subgraph cluster_process {
        label = "Major logic"
        process
    }
    
    start -> process
    process -> end
    
}

subgraph details {
    label = "Process details"
    
    edge [fillcolor="#a6cee3" color="#1f78b4"]
    
    node[shape="ellipse" style="filled" fillcolor="#1f77b4"]
        start1 [label="start"]
        end1 [label="end"]
    node[shape="box" style="filled" fillcolor="#ff7f0e"]
        details
    
    subgraph cluster_details {
        label = "Details"
        details
    }
    
    start1 -> details
    details -> end1
}

}

Note the naming / labels in the second subgraph.

Dot can't render 2 graphs into a single file, the output you see is probably the content of one of the graphs as a png.

In order to prevent that, you may run your graphs first through gvpack - something similar to:

gvpack -u supporting.dot | dot -Tpng -o supporting.png

This combines all graphs in supporting.dot into a single graph, which then is rendered with dot.

The layout of the graphs can be influenced by some more options of gvpack.

It is legal to have multiple graphs defined in one input file. You can then produce multiple output files using the -O option, like this:

dot -Tpng -O multi.gv

This will produce multi.gv.png and multi.gv.2.png

Related