gpt4 book ai didi

graphviz - 在 graphviz 点有向图中,如何打破宽布局(rankdir LR)

转载 作者:行者123 更新时间:2023-12-04 15:50:12 24 4
gpt4 key购买 nike

我试图用 python 生成一个长图,其中始终有一个节点指向下一个节点。这最终导致了节点的长蜗牛(rankdir LR)。但是我想在一定的宽度或数量或节点后打破它。如何实现?

graph = gv.Digraph(format='svg')
graph.graph_attr.update({'rankdir': 'LR'})

graph.node('a', 'A')
graph.node('b', 'B')
graph.node('c', 'C')
graph.node('d', 'D')
graph.node('e', 'E')
graph.node('f', 'F')
...

graph.edges(['ab', 'bc', 'cd', 'de', 'ef', ...])

输出:

all items in one row

但我想要(或类似):

items arranged in two rows with keeping the connection

我尝试使用 size ,但这只会缩放整个图形。

作为解决方法,我尝试减少 ranksep ,但这只会让更多的项目变得更好。

我也搜索了很多,但找不到合适的答案。进入类似方向的一个悬而未决的问题是: graphviz plot too wide .其他相关问题suggested answer was to use invisible elements但这在这里也不起作用。


更新:我根据@vaettchen 的评论更改了边缘代码:

graph.edge('a', 'b', None, {'weight':'5'})
graph.edge('b', 'c', None, {'weight':'5'})
graph.edge('d', 'e', None, {'weight':'5'})
graph.edge('e', 'f', None, {'weight':'5'})

graph.edge('c', 'd', None, {'weight':'1'})
graph.edge('a', 'd', None, {'style':'dashed', 'rank':'same'})

不幸的是,结果现在看起来像这样(样式为“dashed”而不是“invis”以获得更好的可见性): enter image description here

'rank': 'same' 似乎没有任何改变。当应用于节点 A 和 D 时也是如此。

最佳答案

这应该是评论而不是答案,因为它没有解决 python 问题,我猜你也在寻找更“自动”的东西——但也许它提供了一些想法;由于没有其他人接受它,这里有一个纯粹的 graphviz 建议:

digraph so 
{
// graph attributes
rankdir = LR; // horizontal graph
splines = ortho // edges with "corners"

// default/initial node style
node[ shape = box ];

// nodes where the "new lines" begin
// connected invisibly to keep them in order
{ rank = same; A -> E -> I[ style = invis ] }

// nodes that are to be in one line
// extra weight needed to keep the edges straight
edge[ weight = 5 ];
A -> B -> C -> D;
E -> F -> G -> H;
I -> J -> K -> etc;

// edges connecting the graph elements over the lines
edge[ weight = 1 ];
D -> E;
H -> I;
}

产量

enter image description here

关于graphviz - 在 graphviz 点有向图中,如何打破宽布局(rankdir LR),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54133686/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com