Edges between two subgraphs in pydot

旧时模样 提交于 2019-12-10 08:14:19

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!