防止边穿过pygraphviz中的节点

Preventing edges from crossing nodes in 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)

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

这似乎是人们 运行 喜欢的东西,但我在上面找不到任何东西。我可能做错了什么,或者使用了错误的搜索词。任何帮助表示赞赏。

这是因为 graphviz 的 splines attribute

By default, the attribute is unset. How this is interpreted depends on the layout. For dot, the default is to draw edges as splines. For all other layouts, the default is to draw edges as line segments. Note that for these latter layouts, if splines="true", this requires non-overlapping nodes (cf. overlap). If fdp is used for layout and splines="compound", then the edges are drawn to avoid clusters as well as nodes.

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

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