Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GraphViz keep node position with dot

Tags:

graphviz

i know there are some related questions but I am wondering if there are better solutions in forcing graphviz to keep node positions in defined order.

Here is the my Problem: I have two subgraphs with each 5 nodes. Each node is connected to each node of the other graph. I want the nodes to stay in order from 1-1 to 1-5. But graphviz always mixes them up.

Here is the graph, i am using DOT:

digraph G {
rankdir=LR;
ranksep=4.0;
subgraph cluster_1 {
rank=same;
label="Nr:1";
"1-1" [width=1, shape=circle, style=filled, fillcolor="#E3A869", label="1-1"];
"1-2" [width=1, shape=circle, style=filled, fillcolor="#E3A869", label="1-2"];
"1-3" [width=1, shape=circle, style=filled, fillcolor="#E3A869", label="1-3"];
"1-4" [width=1, shape=circle, style=filled, fillcolor="#E3A869", label="1-4"];
"1-5" [width=1, shape=circle, style=filled, fillcolor="#E3A869", label="1-5"];
}

subgraph cluster_2 {
rank=same;
label="Nr:2";
"2-1" [width=1, shape=circle, style=filled, fillcolor="#E3A869", label="2-1"];
"1-1" -> "2-1"  [color=blue, labelfontcolor="#009933", fontsize="10.0", penwidth=1];
"1-2" -> "2-1"  [color=blue, labelfontcolor="#009933", fontsize="10.0", penwidth=1];
"1-3" -> "2-1"  [color=blue, labelfontcolor="#009933", fontsize="10.0", penwidth=1];
"1-4" -> "2-1"  [color=blue, labelfontcolor="#009933", fontsize="10.0", penwidth=1];
"1-5" -> "2-1"  [color=blue, labelfontcolor="#009933", fontsize="10.0", penwidth=1];
"2-2" [width=1, shape=circle, style=filled, fillcolor="#E3A869", label="2-2"];
"1-1" -> "2-2"  [color=blue, labelfontcolor="#009933", fontsize="10.0", penwidth=1];
"1-2" -> "2-2"  [color=blue, labelfontcolor="#009933", fontsize="10.0", penwidth=1];
"1-3" -> "2-2"  [color=blue, labelfontcolor="#009933", fontsize="10.0", penwidth=1];
"1-4" -> "2-2"  [color=blue, labelfontcolor="#009933", fontsize="10.0", penwidth=1];
"1-5" -> "2-2"  [color=blue, labelfontcolor="#009933", fontsize="10.0", penwidth=1];
"2-3" [width=1, shape=circle, style=filled, fillcolor="#E3A869", label="2-3"];
"1-1" -> "2-3"  [color=blue, labelfontcolor="#009933", fontsize="10.0", penwidth=1];
"1-2" -> "2-3"  [color=blue, labelfontcolor="#009933", fontsize="10.0", penwidth=1];
"1-3" -> "2-3"  [color=blue, labelfontcolor="#009933", fontsize="10.0", penwidth=1];
"1-4" -> "2-3"  [color=blue, labelfontcolor="#009933", fontsize="10.0", penwidth=1];
"1-5" -> "2-3"  [color=blue, labelfontcolor="#009933", fontsize="10.0", penwidth=1];
"2-4" [width=1, shape=circle, style=filled, fillcolor="#E3A869", label="2-4"];
"1-1" -> "2-4"  [color=blue, labelfontcolor="#009933", fontsize="10.0", penwidth=1];
"1-2" -> "2-4"  [color=blue, labelfontcolor="#009933", fontsize="10.0", penwidth=1];
"1-3" -> "2-4"  [color=blue, labelfontcolor="#009933", fontsize="10.0", penwidth=1];
"1-4" -> "2-4"  [color=blue, labelfontcolor="#009933", fontsize="10.0", penwidth=1];
"1-5" -> "2-4"  [color=blue, labelfontcolor="#009933", fontsize="10.0", penwidth=1];
"2-5" [width=1, shape=circle, style=filled, fillcolor="#E3A869", label="2-5"];
"1-1" -> "2-5"  [color=blue, labelfontcolor="#009933", fontsize="10.0", penwidth=1];
"1-2" -> "2-5"  [color=blue, labelfontcolor="#009933", fontsize="10.0", penwidth=1];
"1-3" -> "2-5"  [color=blue, labelfontcolor="#009933", fontsize="10.0", penwidth=1];
"1-4" -> "2-5"  [color=blue, labelfontcolor="#009933", fontsize="10.0", penwidth=1];
"1-5" -> "2-5"  [color=blue, labelfontcolor="#009933", fontsize="10.0", penwidth=1];
}

Nodes in wrong order

like image 439
PlagTag Avatar asked Aug 03 '26 23:08

PlagTag


1 Answers

Using some invisible edges to order the nodes within a subgraph, avoiding a LR rank direction and its quirks, using straight edges (splines=false) which do not influence the ranking of the nodes (constraint=false) for the visible blue node connections, applying default styles in order to minimize repetition and reordering the graph markup gets me the following:

digraph G {
    nodesep=4.0;
    splines=false;

    node[width=1, shape=circle, style=filled, fillcolor="#E3A869"];
    edge[style=invis];

    subgraph cluster_1 {
        label="Nr:1";
        "1-1" -> "1-2" -> "1-3" -> "1-4" -> "1-5";
    }

    subgraph cluster_2 {
        label="Nr:2";
        "2-1" -> "2-2" -> "2-3" -> "2-4" -> "2-5";
    }

    edge[style=solid, color=blue, penwidth=1, constraint=false];

    "1-1" -> "2-1";
    "1-2" -> "2-1";
    "1-3" -> "2-1";
    "1-4" -> "2-1";
    "1-5" -> "2-1";

    "1-1" -> "2-2";
    "1-2" -> "2-2";
    "1-3" -> "2-2";
    "1-4" -> "2-2";
    "1-5" -> "2-2";

    "1-1" -> "2-3";
    "1-2" -> "2-3";
    "1-3" -> "2-3";
    "1-4" -> "2-3";
    "1-5" -> "2-3";

    "1-1" -> "2-4";
    "1-2" -> "2-4";
    "1-3" -> "2-4";
    "1-4" -> "2-4";
    "1-5" -> "2-4";

    "1-1" -> "2-5";
    "1-2" -> "2-5";
    "1-3" -> "2-5";
    "1-4" -> "2-5";
    "1-5" -> "2-5";
}

graphivz output with subgraph nodes ordered using invisible edges

like image 105
marapet Avatar answered Aug 07 '26 04:08

marapet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!