Graphviz: Place edge label on the other side (II)

Hotschke picture Hotschke · Aug 29, 2013 · Viewed 20.6k times · Source

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.

Answer

Richard EB picture Richard EB · Sep 9, 2013

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.