如何在我的python anytree graphviz输出中反转箭头的方向?

问题描述 投票:0回答:1

我正在使用python中的anytree包构建旨在表示从叶子到树的根的流的树。我有以下代码。

from anytree import Node
from anytree.exporter import DotExporter
A = Node('A')
B = Node('B', parent = A)
C = Node('C', parent = A)
DotExporter(A).to_picture('example.png')

并产生以下图像。

enter image description here

我想修改此图像,以使箭头指向相反的方向。我知道在graphviz中将[dir=back]添加到边缘定义线会给我想要的结果。通过运行以下代码:

for line in DotExporter(A):
    print(line)

我得到输出:

digraph tree {
    "A";
    "B";
    "C";
    "A" -> "B";
    "A" -> "C";
}

但是如何修改anytree界面的DotExporter输出以将[dir=back]添加到边缘定义线并反转箭头的方向?

python graphviz dot anytree
1个回答
0
投票

要修改DotDotExporter定义的边缘属性,您需要照此更改edgeattrfuncDotExporter属性。

from anytree import Node
from anytree.exporter import DotExporter
A = Node('A')
B = Node('B', parent = A)
C = Node('C', parent = A)
DotExporter(A, edgeattrfunc = lambda node, child: "dir=back").to_picture('example.png')

查看输出,您现在得到:

for line in DotExporter(A, edgeattrfunc = lambda node, child: "dir=back"):
    print(line)
digraph tree {
    "A";
    "B";
    "C";
    "A" -> "B" [dir=back];
    "A" -> "C" [dir=back];
}

将产生以下图像。

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.