5

With pygraphviz, I can add subgraphs with add_subgraph(list_of_nodes, label='cluster_somename'). This will create it inside a subgraph block when layout is called.

Is there a way to nest the subgraphs?

I'm using the dot layout, I know it can handle and display subclusters if they are nested. But I can't get pygraphviz to output nested clusters.

KalEl
  • 8,978
  • 13
  • 47
  • 56

1 Answers1

2

You can call the subgraph's add_subgraph() to create a nested subgraph.

import pygraphviz as pgv

g = pgv.AGraph(name='root')
g.add_node('A')

g.add_subgraph(name='cluster_1')
c1 = g.subgraphs()[-1]
c1.add_node('B')

c1.add_subgraph(name='cluster_2')
c2 = c1.subgraphs()[-1]
c2.add_node('C')

print(g)
strict graph root {
    subgraph cluster_1 {
        subgraph cluster_2 {
            C;
        }
        B;
    }
    A;
}
kzm4269
  • 39
  • 2