Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

graphviz

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.

like image 436
Hotschke Avatar asked Sep 04 '25 01:09

Hotschke


1 Answers

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.

like image 134
Richard EB Avatar answered Sep 07 '25 19:09

Richard EB



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!