2

With Graphviz, I know I can label edges and vertices. But what if I want to label some of the connections? That is, label some points where a specific edge meets a specific vertex? Is that possible?

Notes:

  • I don't care about beatuy/visual styles, I'll take something plain and dirty(ish)
  • I would very much rather not use "tricks" such as spacing out the vertex label to make some of the text appear next to the connection points and similar approaches.
IInspectable
  • 46,945
  • 8
  • 85
  • 181
einpoklum
  • 118,144
  • 57
  • 340
  • 684

2 Answers2

1

You can use the taillabel and headlabel edges attributes. For example:

digraph G {
        rankdir="LR";
        a->b [taillabel="from"; headlabel="to"]
        b->c [taillabel="from"; headlabel="to"]
}

produce:

example

Ohad Eytan
  • 8,114
  • 1
  • 22
  • 31
  • Oh, that's just what I needed, great :-) ... is there also some way to "space out" the edge, or better control where the tail and head labels are placed? Also, is there a specific reason you set `rankdir="LR"` or is it just to get the right-to-left effect for your specific example? – einpoklum Aug 07 '16 at 11:35
  • 1
    You're welcome. The `rankdir` is just for the example. You can use `minlen` for more long edges – Ohad Eytan Aug 07 '16 at 12:58
0

A clear way to label where something is leaving or coming into a node is to use "port"-like constructs inside the source/target nodes. Some ways to achieve this:

subgraph.dot

digraph {
  subgraph cluster_house {
    label="House"
    hcw [label="Clean water"]
    hdw [label="Dirty water"]
  }
  subgraph cluster_street {
    label="Street"
    scw [label="Clean water"]
    sdw [label="Dirty water"]
  }
  scw -> hcw
  hdw -> sdw
}

enter image description here

Docs: https://graphviz.org/docs/attrs/cluster/

struct.dot

digraph structs {
    node [shape=record];
    rankdir=LR
    house [label="<l> House|<cw> Clean water|<dw> Dirty water"];
    street [label="<l> Street|<cw> Clean water|<dw> Dirty water"];
    street:cw -> house:cw;
    house:dw -> street:dw;
}

enter image description here

Docs: https://graphviz.org/doc/info/shapes.html#record

I don't know how to make the headers stand out however with this approach: graphviz: record node with a bold title

html.dot

digraph structs {
    node [shape=plaintext]
    house [label=<
<table border="0" cellborder="1" cellspacing="0">
  <th><td><b>House</b></td></th>
  <tr><td port="cw">Clean water</td></tr>
  <tr><td port="dw">Dirty water</td></tr>
</table>>];
    street [label=<
<table border="0" cellborder="1" cellspacing="0">
  <th><td><b>Street</b></td></th>
  <tr><td port="cw">Clean water</td></tr>
  <tr><td port="dw">Dirty water</td></tr>
</table>>];
    street:cw -> house:cw;
    house:dw -> street:dw;
}

enter image description here

Docs: https://graphviz.org/doc/info/shapes.html#html

Tested on Graphviz 2.42.2, Ubuntu 22.04.

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985