如何在pydot中为两个子图添加边?

4 投票
1 回答
5430 浏览
提问于 2025-04-16 22:57

有没有人知道怎么在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)

我试过这个:

callgraph.add_edge(pydot.Edge("foo","bar"))

但是不行。它只是创建了两个新的节点,分别叫“foo”和“bar”,然后在它们之间加了一条边!

有没有人能帮帮我?

1 个回答

6
  • Graphviz要求边必须连接两个不同的节点。
  • 添加图的参数compound='true'。
  • 使用边的参数lhead=和ltail=。

所以你的代码应该变成:

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()))

撰写回答