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?
            Asked
            
        
        
            Active
            
        
            Viewed 1.1e+01k times
        
    4 Answers
295
            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.

 
    
    
        Martijn Pieters
        
- 1,048,767
- 296
- 4,058
- 3,343
 
    
    
        Andrew Walker
        
- 40,984
- 8
- 62
- 84
- 
                    48Why are the labels touching the edges? Shouldn't there be a gap? – AndreKR Jan 20 '11 at 22:27
- 
                    13@AndreKR A quick fix is to just put a space at the beginning of the label: a -> b [ label=" a to b" ]; – chembrad Jan 27 '12 at 21:30
- 
                    2This duplicates the definitions a lot. Is there a way to do something like that: `a - "a to b" > b - "b to c" > c` ? – user569825 Mar 19 '12 at 17:34
- 
                    7Another fix is to use `rankdir="LR";`, which produces a horizontal graph with labels placed above the edge without touching. – Dave Jarvis Nov 19 '12 at 23:23
- 
                    8is there a way to have the labels rotated vertically to go paralle to the line? – Justin L. Dec 08 '13 at 02:42
- 
                    I don't believe there's any need to declare a b or c before describing edges, is there? – Russia Must Remove Putin Mar 28 '14 at 20:41
- 
                    @Potherca Ha! Cool, I suppose, typically shouldn't edit other people's code, but in this case, probably fine. Good job, cheers! – Russia Must Remove Putin Sep 04 '14 at 14:55
- 
                    2Regarding 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? – user2023370 Mar 07 '15 at 00:58
- 
                    7For future reference, the DOT language documentation is [here](http://www.graphviz.org/doc/info/lang.html) and the attributes documentation is [here](http://www.graphviz.org/doc/info/attrs.html). – Jens Sep 14 '16 at 00:50
- 
                    I wish the syntax was more like `graph-easy`: https://github.com/ironcamel/Graph-Easy/wiki#hello-world – Sridhar Sarnobat Apr 05 '22 at 21:12
40
            
            
        @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" ];
}
 
    
    
        Allan Bowe
        
- 12,306
- 19
- 75
- 124
- 
                    1
- 
                    Which version did you try with? With version dot - graphviz version 2.43.0 (0) tooltip does not show up. – Yu Shen Aug 06 '23 at 01:34
24
            
            
        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"]
 
    
    
        Rudolf Real
        
- 1,948
- 23
- 27
10
            
            
        You can use label="\E" It will generate bye default label. 
For Example:
digraph G {
 a -> b [ label="\E" ];
 b -> c [ label="\E"];
}
 
    
    
        Nirav Patel
        
- 1,297
- 1
- 12
- 23
 
     
    
