Search code examples
positioninggraphvizsubgraph

Graphviz subgraph alignment issue


I am trying to force the nodes to have a specified position in the graph. While doing so, the different sub-graphs are not aligned properly. The code to generate this graph is :

digraph {
rankdir=LR;
labeljust="l";

subgraph cluster0{

label="t=0" 


n_3_0_0[label="192.168.8.6"
    pos="15,12!"    
    ];
n_3_0_1[label="192.168.8.3"
    pos="10,10!"    
    ];

 n_3_0_0 -> n_3_0_1 ;
 n_3_0_1 -> n_3_0_0 ;
};

subgraph cluster1{

label="t=5"     

n_3_1_0[label="192.168.8.6"
    pos="15,12!"    
    ];
n_3_1_1[label="192.168.8.3"
    pos="10,10!"    
    ];
n_3_1_2[label="192.168.8.9"
    pos="10,12!"    
    ];

 n_3_1_0 -> n_3_1_1 ;
 n_3_1_1 -> n_3_1_0 ;
};


subgraph cluster2{

label="t=10" 

n_3_2_0[label="192.168.8.6"
    pos="14,10!"    
    ];
n_3_2_1[label="192.168.8.3"
    pos="10,10!"    
    ];
n_3_2_2[label="192.168.8.9"
    pos="15,11!"    
    ];
n_3_2_3[label="192.168.8.8"
    pos="18,12!"    
    ];

 n_3_2_0 -> n_3_2_1 ;

 n_3_2_2 -> n_3_2_3 ;
 n_3_2_1 -> n_3_2_0 ;
 n_3_2_3 -> n_3_2_2 ;
};

}

I compiled this code by forcing the node position:

dot -Kfdp -n -Tpng -o sample.png test2.dot

The output graph is: http://imageshack.us/photo/my-images/826/samplebg.png/ The problem with the output I got is: 1. the subgraph are NOT displayed in sequence of t=0, t-5, t=10... 2. the subgraph are NOT aligned to left.

I need to have output graph like this: http://imageshack.us/photo/my-images/253/needed.png/

Thnak You.


Solution

  • Since you already precalculated the positions of all the nodes, you don't really need fdp - you just nead a layout which respects the pos attribute.

    So you could generate the output like this:

    neato -Tpng sourcedot.gv -O
    

    But you'd have to adjust node positions before that, in order to have the subgraphs correctly stacked and not super-posed:

    digraph {
    rankdir=LR;
    labeljust="l";
    
    subgraph cluster0{
    label="t=0"
    n_3_0_0[label="192.168.8.6"
        pos="15,4!"
        ];
    n_3_0_1[label="192.168.8.3"
        pos="10,2!"
        ];
    
     n_3_0_0 -> n_3_0_1 ;
     n_3_0_1 -> n_3_0_0 ;
    };
    
    subgraph cluster1{
    label="t=5"
    n_3_1_0[label="192.168.8.6"
        pos="15,7!"
        ];
    n_3_1_1[label="192.168.8.3"
        pos="10,5!"
        ];
    n_3_1_2[label="192.168.8.9"
        pos="10,7!"
        ];
    
     n_3_1_0 -> n_3_1_1 ;
     n_3_1_1 -> n_3_1_0 ;
    };
    
    
    subgraph cluster2{
    label="t=10"
    n_3_2_0[label="192.168.8.6"
        pos="14,8!"
        ];
    n_3_2_1[label="192.168.8.3"
        pos="10,8!"
        ];
    n_3_2_2[label="192.168.8.9"
        pos="15,9!"
        ];
    n_3_2_3[label="192.168.8.8"
        pos="18,10!"
        ];
    
     n_3_2_0 -> n_3_2_1 ;
    
     n_3_2_2 -> n_3_2_3 ;
     n_3_2_1 -> n_3_2_0 ;
     n_3_2_3 -> n_3_2_2 ;
    };
    
    }
    

    Resulting in

    enter image description here (Some minor adjustments still needed for the label of the middle subgraph)

    Or just use dot and let it align the nodes, too:

    enter image description here