问题
Does anyone know how to add an edge between two subgraphs (clusters) in pydot?
callgraph = pydot.Dot(graph_type='digraph',fontname="Verdana")
cluster_foo=pydot.Cluster('foo',label='foo')
cluster_foo.add_node(pydot.Node('foo_method_1',label='method_1'))
callgraph.add_subgraph(cluster_foo)
cluster_bar=pydot.Cluster('bar',label='Component1')
cluster_bar.add_node(pydot.Node('bar_method_a'))
callgraph.add_subgraph(cluster_bar)
I tried:
callgraph.add_edge(pydot.Edge("foo","bar"))
but doesn't work. It just creates two more nodes labeled "foo" and "bar" in the initial graph and put's and edge between them!
Can anyone help, please?
Thanks!
回答1:
- Graphviz demands that an edge be between nodes in the 2 clusters.
- Add graph parameter compound='true'.
- Use edge parameters lhead= and ltail=.
So your code would become:
callgraph = pydot.Dot(graph_type='digraph', fontname="Verdana", compound='true')
cluster_foo=pydot.Cluster('foo',label='foo')
callgraph.add_subgraph(cluster_foo)
node_foo = pydot.Node('foo_method_1',label='method_1')
cluster_foo.add_node(node_foo)
cluster_bar=pydot.Cluster('bar',label='Component1')
callgraph.add_subgraph(cluster_bar)
node_bar = pydot.Node('bar_method_a')
cluster_bar.add_node(node_bar)
callgraph.add_edge(pydot.Edge(node_foo, node_bar, ltail=cluster_foo.get_name(), lhead=cluster_bar.get_name()))
来源:https://stackoverflow.com/questions/6953957/edges-between-two-subgraphs-in-pydot