dot is a wonderful tool and the node xlabel attribute allows for an “external label” in addition to the normal internal node label.  Placement is always at the top left of the node’s bounding box which isn’t always convenient.  There’s no way to change this, a bit of chatter in various user groups about it, and it’s on the future feature list.

If you’re in a hurry you can hack the source.  It was my first foray into the code base but it didn’t take too long to find out what to do.  I’m working off graphviz-2.40.1 which you can download from the Graphviz site.

In lib/common/postproc.c starting at line 159 I made the following changes which place the xlabel at the lower left.  The original code is shown commented out, followed by the replacement code.


/* HACK
    if (ND_xlabel(v))
    ND_xlabel(v)->pos = map_point(ND_xlabel(v)->pos);
*/
    if (ND_xlabel(v)) {
        pointf p;
        p = map_point(ND_xlabel(v)->pos);
        p.y -= (ND_ht(v) + 0.5*ND_xlabel(v)->dimen.y);
        p.x += ND_xlabel(v)->dimen.x;
        ND_xlabel(v)->pos = p;
     }

You can adjust the positioning in the x- and y-directions as you see fit:

  • ND_ht(v), is the height of the node’s bounding box
  • ND_xlabel(v)->dimen.y, is the height of the text label
  • ND_xlabel(v)->dimen.x, is the width of the text label

This is a cheap and cheerful hack and can’t guarantee that an xlabel won’t lie on top of an edge.

For more info about dot check out the graphviz project.

Hacking graphviz/dot to place xlabels