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
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.

11 Comments
AndreKR
Why are the labels touching the edges? Shouldn't there be a gap?
chembrad
@AndreKR A quick fix is to just put a space at the beginning of the label: a -> b [ label=" a to b" ];
Dave Jarvis
Another fix is to use
rankdir="LR";, which produces a horizontal graph with labels placed above the edge without touching.Justin L.
is there a way to have the labels rotated vertically to go paralle to the line?
|
@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" ];
}
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
Community
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.

