How to force node position (x and y) in graphviz

PositionGraph TheoryGraphvizDot

Position Problem Overview


I am trying to force position of nodes. I have x and y coordinates of my nodes and its also directed graph. I can use the rank=same to handle row (y coordinate), but can't figure out how I can handle column (x coordinate).

Position Solutions


Solution 1 - Position

You can use pos attribute (https://www.graphviz.org/doc/info/attrs.html#d:pos), e.g.:

xxx [	label = xxx	pos = "0,0!"]

yyy [	label = yyy	pos = "10,10!"]

You will also have to specify neato or fdp layout engine, so that dot command-line would be (for fdp):

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

Solution 2 - Position

Here is an example I found: https://observablehq.com/@magjac/placing-graphviz-nodes-in-fixed-positions

Essentially the position attribute "pos" can be specified for a node. Only works with neato or fdp layout engines, not dot.

The ! indicates that the position is an input and should not be altered.

Solution 3 - Position

I couldn't get the -n flag work with dot -Kfdp.

I was however able to get it working with neato using the following command:

neato sample.dot -n -Tpng -o sample.png

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser664947View Question on Stackoverflow
Solution 1 - PositionAndreyView Answer on Stackoverflow
Solution 2 - PositionJP ThomasView Answer on Stackoverflow
Solution 3 - PositionJaakkoView Answer on Stackoverflow