Search code examples
graphvizneato

Arrange outer nodes as square


Considering the following graph:

graph {
  node[shape=square];
  A [color=olivedrab4 /* pos="0,0!" */ ];
  B [color=orange     /* pos="0,3!" */ ];
  C [color=royalblue  /* pos="5,3!" */ ];
  D [color=orangered  /* pos="5,0!" */ ];

  node[shape=circle];
  u -- v -- w -- {x y}

  A -- u;
  B -- v;
  C -- y;
  D -- x;

}

which neato renders like this:

freely arranged graph

is there a way to fix [A-D] in the corners of a square and have the subgraph [u-y] neatly laid out between?

Note: Uncommenting the position hints in the code above yields the following, unsatisfactory result:

position hinted graph


Solution

  • Simply use fdp instead of neato and uncomment the position hints:

    fdp -Tpng corners.gv -o corners.png
    

    graphviz output

    Or, if you want to use neato, add some initial pos attribute to the other nodes, too, and place them in the middle:

    node[shape=circle, pos="2.5,1.5"];
    

    (no exclamation mark after the coordinates).

    Output is almost the same:

    graphviz output - neato