How to add edge labels in Graphviz?
Asked Answered
A

4

227

I am trying to draw a graph using Graphviz, but I need to add labels on the edges. There does not seem to be any way to that in Graphviz. Are there a way out?

Anthropocentric answered 27/11, 2009 at 5:8 Comment(0)
E
302

You use the label property attached to the edge.

digraph G {
 a -> b [ label="a to b" ];
 b -> c [ label="another label"];
}

The above generates a graph that looks something like this.

alt text

Elson answered 27/11, 2009 at 5:11 Comment(11)
Why are the labels touching the edges? Shouldn't there be a gap?Acus
@Acus A quick fix is to just put a space at the beginning of the label: a -> b [ label=" a to b" ];Clingy
This duplicates the definitions a lot. Is there a way to do something like that: a - "a to b" > b - "b to c" > c ?Underplay
Another fix is to use rankdir="LR";, which produces a horizontal graph with labels placed above the edge without touching.Emmalynn
is there a way to have the labels rotated vertically to go paralle to the line?Pash
I don't believe there's any need to declare a b or c before describing edges, is there?Bismuthic
@Potherca Ha! Cool, I suppose, typically shouldn't edit other people's code, but in this case, probably fine. Good job, cheers!Bismuthic
Regarding the labels touching the edges...the work-arounds mentioned seem a little ad-hoc, and also rely on axis aligned graph edges. Labels seem often to touch the edges with complex graphs. Is there no general solution?Harebrained
For future reference, the DOT language documentation is here and the attributes documentation is here.Appropriate
I wish the syntax was more like graph-easy: github.com/ironcamel/Graph-Easy/wiki#hello-worldSidwel
Should we replace weight with label than?Hammerless
B
42

@Andrew Walker has given a great answer!

It's also worth being aware of the labeltooltip attribute. This allows an additional string to be attached to the label of an edge. This is easier for a user than the tooltip attribute, as it can be fiddly to hover directly on an edge. The syntax is as follows:

digraph G {
 a -> b [label="  a to b" labeltooltip="this is a tooltip"];
 b -> c [label="  another label" ];
}

Which gives the following result: example of a label with tooltip

Bigham answered 9/1, 2017 at 15:46 Comment(3)
Tooltip hint invaluable. Thanks.Bondwoman
Which version did you try with? With version dot - graphviz version 2.43.0 (0) tooltip does not show up.Indian
Facing the same problem (using Graphviz Macport version 9.0.0 (20230911.1827)).Rubious
R
26

Landed here by googling whether labels could be on arrow's ends, for UML's composition/aggregation. The answer is yes:

"Person" -> "Hand" [headlabel="*", taillabel="1"]

enter image description here

Roos answered 15/10, 2020 at 17:53 Comment(0)
M
11

You can use label="\E" It will generate bye default label.

For Example:

digraph G {
 a -> b [ label="\E" ];
 b -> c [ label="\E"];
}
Meza answered 29/5, 2018 at 18:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.