235

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?

5 Answers 5

316

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

Sign up to request clarification or add additional context in comments.

11 Comments

Why are the labels touching the edges? Shouldn't there be a gap?
@AndreKR A quick fix is to just put a space at the beginning of the label: a -> b [ label=" a to b" ];
Another fix is to use rankdir="LR";, which produces a horizontal graph with labels placed above the edge without touching.
is there a way to have the labels rotated vertically to go paralle to the line?
For future reference, the DOT language documentation is here and the attributes documentation is here.
|
46

@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

2 Comments

Which version did you try with? With version dot - graphviz version 2.43.0 (0) tooltip does not show up.
Facing the same problem (using Graphviz Macport version 9.0.0 (20230911.1827)).
32

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

Comments

11

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

For Example:

digraph G {
 a -> b [ label="\E" ];
 b -> c [ label="\E"];
}

Comments

1
digraph G {
    rankdir=LR;
    
    User [label="User"];
    WebApp [label="Web Application"];
    Model [label="Model"];
    
    User -> WebApp [label="Register/Login"];
    WebApp -> User [label="Response"];
    User -> WebApp [label="Upload Image"];
    WebApp -> Model [label="Preprocess Image"];
    Model -> WebApp [label="Image Ready"];
    WebApp -> Model [label="Predict Image Class"];
    Model -> WebApp [label="Prediction Response"];
    WebApp -> User [label="Display Result"];
}

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.