user855
user855

Reputation: 19948

How to add edge labels in Graphviz?

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?

Upvotes: 233

Views: 117805

Answers (5)

Proctor MCS
Proctor MCS

Reputation: 11

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"];
}

Upvotes: 1

Rudolf Real
Rudolf Real

Reputation: 2028

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

Upvotes: 31

Nirav Patel
Nirav Patel

Reputation: 1327

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

For Example:

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

Upvotes: 11

Andrew Walker
Andrew Walker

Reputation: 42500

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

Upvotes: 311

Allan Bowe
Allan Bowe

Reputation: 12701

@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

Upvotes: 45

Related Questions