防止边缘穿过pygraphviz中的节点

时间:2015-11-20 02:07:10

标签: python pygraphviz

我正在阅读一个JSON文件并使用pygraphviz动态创建图形,使用一个简单的循环:

hostdata = []
nodes = []
edges = {}
current_host = ""
trial = pgv.AGraph(strict=False, overlap=False)
for filename in os.listdir(options.directory):
    with open(options.directory + "/" + filename, "r") as myfile:
        hostdata = Truth(myfile.read().replace('\n', ''))
    nodes.append(hostdata.host["something"])
    current_something = hostdata.host["something"]
    for key, value in hostdata.peer.iteritems():
        nodes.append(key)
        edges[current_something] = key
        trial.add_edge(current_host, key)

图表很复杂,但我真的更喜欢边缘不穿过节点。我试过,当我设置严格和重叠时,但我仍然有越过节点的线。

Graph with crossed nodes

这似乎是人们会遇到很多事情,但我找不到任何东西。我可能做了一些完全错误的事情,或使用了错误的搜索词。任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:6)

这是因为graphviz的splines attribute而发生的。

  

默认情况下,该属性未设置。如何解释这取决于布局。对于点,默认设置是将边绘制为样条曲线。对于所有其他布局,默认设置是将边绘制为线段。请注意,对于后面这些布局,如果splines =“true”,则需要非重叠节点(参见重叠)。如果fdp用于布局并且splines =“compound”,则绘制边以避免簇和节点。

将其作为命名参数提供应解决问题:

trial = pgv.AGraph(strict=False, overlap=False, splines='true')
#or   
trial = pgv.AGraph(strict=False, overlap=False, splines='spline')