python中graphviz中的图形节点位置

我一直在寻找一段时间来在
python中的graphviz中指定图形中的节点位置.我在dot中找到了一个子图的
rank command,这是我正在寻找的,但是我找不到在python中将graphgraph中的子图和等级组合起来的方法.我也试图强制节点位置,但也没有工作.我创建了一个我想要实现的简单示例.

这是我的代码:

from graphviz import Digraph

top_nodes = ['a', 'b', 'c']

other_nodes = ['d', 'e', 'f', 'g', 'm', 'n']

g = Digraph('test', format='png')

for n in top_nodes:
    g.node(str(n), color='red')

for n in other_nodes:
    g.node(str(n))

g.edge('a', 'd')
g.edge('d', 'g')
g.edge('g', 'm')
g.edge('m', 'n')
g.edge('b', 'e')
g.edge('b', 'f')
g.edge('e', 'n')
g.edge('c', 'f')

g.view()

这是输出:

我希望红色节点(“源”)位于同一级别的图形顶部,只要保留分层布局,其余节点的位置就不那么重要了.

最佳答案 尝试rank = same语句

《python中graphviz中的图形节点位置》

digraph G {
a b c d e f g m n
{rank = same; a; b; c;}
a->d
d->g
g->m
m->n
b->e
b->f
e->n
c->f
}
点赞