26

I need to print a large number of graphs using Graphviz DOT. To distinguish which input each graph corresponds to, I want to also have a caption for each graph. Is there anyway to embed this into the DOT representation of the graphs.

myahya
  • 3,079
  • 7
  • 38
  • 51

3 Answers3

48

You can use label to add a caption to the graph.

Example:

digraph {
    A -> B;
    label="Graph";
    labelloc=top;
    labeljust=left;
}

labelloc and labeljust can be used to determine top/bottom and left/right position of the graph label.

All the details and other attributes that can be used to modify the label (font etc) in the graphviz attribute reference.

Tip: Define the graph label end of your dot file, otherwise subgraphs will inherit those properties.

marapet
  • 54,856
  • 12
  • 170
  • 184
11

Graph's can have attributes just like nodes and edges do:

digraph {
    graph [label="The Tale of Two Cities", labelloc=t, fontsize=30];
    node [color=blue];
    rankdir = LR;
    London -> Paris;
    Paris -> London;
}

That dot file produces this graph.

enter image description here

Mohan Radhakrishnan
  • 3,002
  • 5
  • 28
  • 42
Raymond Hettinger
  • 216,523
  • 63
  • 388
  • 485
2

If you are looking for a way to add a caption to a Graph object of graphviz in python. Then the following code can help:

from graphviz import Graph
dot = Graph()
dot.node('1','1')
dot.node('2','2')
dot.edge('1','2', label="link")

dot.attr(label="My caption")
dot.attr(fontsize='25')

dot.render(view=True)

Output:

output

Ritwik
  • 521
  • 7
  • 17