25

related questions are:

Graphviz: Place edge label on the other side

How to place edge labels ON edge in graphviz

Consider following dot file:

digraph {
0 -> 1 [ len=2, label="(1, 0)"];
0 -> 1 [ len=0.5, dir=none, weight=10];
1 -> 0 [ len=2, label="(0, -1)"];
}

giving following result:

enter image description here

How can I manage to get a symmetric version? (0,-1) should be left of the right egde.

Community
  • 1
  • 1
Hotschke
  • 9,402
  • 6
  • 46
  • 53

1 Answers1

29

As you've found out, graphviz doesn't let you choose horizontal label placement, so all solutions are slightly hacky.

Attempt #1: The two solutions posted by marapet (here)

  1. The labelangle and labeldistance trick doesn't adapt well to different lengths of label text (you'd have to recalculate new distance/angle numbers).

  2. The splines=false trick doesn't work so well where the number of edges between nodes > the number of nodes (you end up with overlapping edges).

Attempt #2: xlabels and anchors to create curved edges

This uses a relatively new feature of graphviz, xlabel (which places the label AFTER the coordinates for the nodes/edges have been decided). The ports feature is used to create curved edges. The padding on the labels is achieved with space characters.

gv

digraph {
forcelabels=true;

    0:sw -> 1:nw [ dir=forward, xlabel="  (1, 0)  "];
    0 -> 1 [dir=none];
    1:ne -> 0:se [ dir=backward, xlabel= "  (0, -1)  "];

}

I believe you need graphviz version >2.29 to use xlabel.

Community
  • 1
  • 1
Richard EB
  • 967
  • 10
  • 24
  • 1
    In my version of dot (2.40) it no longer seems to be possible to use spaces to control the label position. – N. Virgo Dec 08 '17 at 08:29
  • 1
    @Nathaniel, in case it helps try to prepend/append some nearly unseen character, like the dot (`.`) or something else in the Unicode table. This might then register as a character the layout engine will use including your spaces: `xlabel=". mytext"` or `xlabel="mytext ."`. – AlexGrafe Feb 28 '18 at 13:54