gpt4 book ai didi

python - Pygraphviz/networkx 设置节点级别或层

转载 作者:行者123 更新时间:2023-12-02 08:40:01 24 4
gpt4 key购买 nike

我有一个代表一种家谱树的数据集。每个节点有 2 个父节点(除了第一代,它们没有父节点)。对于给定节点,其父节点可以来自任何上一代。例如,第 n 代的节点可以在 n-1 中有一个父节点,在 n-5 中有另一个父节点。一个节点可以是其他几个节点的父节点。

所以基本上,对于每个节点,我都知道它的代和它的父节点。

我试图表示这个图,使来自同一代的节点保持在同一行。除了第一代,每一代都有 10 个节点。

到目前为止,我正在尝试“点”布局。当我只输入两代时,它做得很好,但是当我输入3代时,它“太聪明了”,并且以某种方式重新排列了节点。

比如下面的数据,代表3代:

[(的ObjectId( '530b2ad783a1a15e695c9711'),的ObjectId( '530b804883a1a15e695c972b')),(的ObjectId( '530b2ad783a1a15e695c971c'),的ObjectId( '530b804883a1a15e695c972b')),(的ObjectId( '530b2ad783a1a15e695c9709'),的ObjectId( '530b804883a1a15e695c9727')),(的ObjectId( '530b2ad783a1a15e695c970a'),的ObjectId( '530b804883a1a15e695c9727')),(的ObjectId( '530b2ad783a1a15e695c970b'),的ObjectId( '530b804883a1a15e695c9724')),(的ObjectId( '530b2ad783a1a15e695c970f'),的ObjectId( '530b804883a1a15e695c9724')),(的ObjectId( '530b2ad783a1a15e695c9714'),的ObjectId( '530b804883a1a15e695c9729')),(的ObjectId( '530b2ad783a1a15e695c970e'),的ObjectId( '530b804883a1a15e695c9729')),(的ObjectId( '530b2ad783a1a15e695c9711'),的ObjectId( '530b804883a1a15e695c9723')),(的ObjectId('530b2ad783a1a15e695c970e '),的ObjectId(' 530b804883a1a15e695c9723 ')),(的ObjectId(' 530b2ad783a1a15e695c971c '),的ObjectId(' 530b804883a1a15e695c9728 ')),(的ObjectId(' 530b2ad783a1a15e695c9719 '),的ObjectId(' 530b804883a1a15e695c9728 ')),(的ObjectId(' 530b2ad783a1a15e695c9714' ) , ObjectId('530b804883a1a1 5e695c9726 ')),(的ObjectId(' 530b2ad783a1a15e695c9713 '),的ObjectId(' 530b804883a1a15e695c9726 ')),(的ObjectId(' 530b2ad783a1a15e695c9716 '),的ObjectId(' 530b804883a1a15e695c9722 ')),(的ObjectId(' 530b2ad783a1a15e695c9719 '),的ObjectId(' 530b804883a1a15e695c9722' )),(的ObjectId( '530b2ad783a1a15e695c970d'),的ObjectId( '530b804883a1a15e695c9725')),(的ObjectId( '530b2ad783a1a15e695c9715'),的ObjectId( '530b804883a1a15e695c9725')),(的ObjectId( '530b804883a1a15e695c9724'),的ObjectId( '530ba27c83a1a15e695c972d')) ,(的ObjectId( '530b2ad783a1a15e695c9713'),的ObjectId( '530ba27c83a1a15e695c972d')),(的ObjectId( '530b804883a1a15e695c9724'),的ObjectId( '530ba27c83a1a15e695c972e')),(的ObjectId( '530b2ad783a1a15e695c9709'),的ObjectId( '530ba27c83a1a15e695c972e')),(的ObjectId( '530b804883a1a15e695c9727'),的ObjectId( '530ba27c83a1a15e695c972f')),(的ObjectId( '530b2ad783a1a15e695c9709'),的ObjectId( '530ba27c83a1a15e695c972f')),(的ObjectId( '530b2ad783a1a15e695c9713'),的ObjectId( '530ba27c83a1a15e695c9730')),(的ObjectId( '530b2ad783a1a15e695c9709'), ObjectId(' 530ba27c83a1a15e695c9730 ')),(的ObjectId(' 530b2ad783a1a15e695c9713 '),的ObjectId(' 530ba27c83a1a15e695c9731 ')),(的ObjectId(' 530b804883a1a15e695c9725 '),的ObjectId(' 530ba27c83a1a15e695c9731 ')),(的ObjectId(' 530b804883a1a15e695c9722 '),的ObjectId(' 530ba27c83a1a15e695c9732' )),(的ObjectId( '530b2ad783a1a15e695c970b'),的ObjectId( '530ba27c83a1a15e695c9732')),(的ObjectId( '530b2ad783a1a15e695c9711'),的ObjectId( '530ba27c83a1a15e695c9733')),(的ObjectId( '530b2ad783a1a15e695c971f'),的ObjectId( '530ba27c83a1a15e695c9733')) ,(的ObjectId( '530b804883a1a15e695c972b'),的ObjectId( '530ba27c83a1a15e695c9734')),(的ObjectId( '530b2ad783a1a15e695c9713'),的ObjectId( '530ba27c83a1a15e695c9734')),(的ObjectId( '530b2ad783a1a15e695c9709'),的ObjectId( '530ba27c83a1a15e695c9735')),(的ObjectId( '530b804883a1a15e695c9724'),的ObjectId( '530ba27c83a1a15e695c9735')),(的ObjectId( '530b2ad783a1a15e695c9713'),的ObjectId( '530ba27c83a1a15e695c9736')),(的ObjectId( '530b804883a1a15e695c9723'),的ObjectId( '530ba27c83a1a15e695c9736')),(的ObjectId( '530b2ad783a1a15e695c9716 '),的ObjectId(' 530b804883a1a15e695c9722 ')),(的ObjectId(' 530b2ad783a1a15e695c9719 '),的ObjectId(' 530b804883a1a15e695c9722 ')),(的ObjectId(' 530b2ad783a1a15e695c9711 '),的ObjectId(' 530b804883a1a15e695c9723 ')),(的ObjectId(' 530b2ad783a1a15e695c970e') ,的ObjectId( '530b804883a1a15e695c9723')),(的ObjectId( '530b2ad783a1a15e695c970b'),的ObjectId( '530b804883a1a15e695c9724')),(的ObjectId( '530b2ad783a1a15e695c970f'),的ObjectId( '530b804883a1a15e695c9724')),(的ObjectId( '530b2ad783a1a15e695c970d'),的ObjectId ( '530b804883a1a15e695c9725')),(的ObjectId( '530b2ad783a1a15e695c9715'),的ObjectId( '530b804883a1a15e695c9725')),(的ObjectId( '530b2ad783a1a15e695c9714'),的ObjectId( '530b804883a1a15e695c9726')),(的ObjectId( '530b2ad783a1a15e695c9713'),的ObjectId(” 530b804883a1a15e695c9726 ')),(的ObjectId(' 530b2ad783a1a15e695c9709 '),的ObjectId(' 530b804883a1a15e695c9727 ')),(的ObjectId(' 530b2ad783a1a15e695c970a '),的ObjectId(' 530b804883a1a15e695c9727 ')),(的ObjectId(' 530b2ad783a1a15e695c971c '),的ObjectId(' 530b804883a1a15e695c9728' )), (ObjectId('530b2ad783 a1a15e695c9719 '),的ObjectId(' 530b804883a1a15e695c9728 ')),(的ObjectId(' 530b2ad783a1a15e695c9714 '),的ObjectId(' 530b804883a1a15e695c9729 ')),(的ObjectId(' 530b2ad783a1a15e695c970e '),的ObjectId(' 530b804883a1a15e695c9729 ')),(的ObjectId(' 530b2ad783a1a15e695c9715' ),的ObjectId( '530b804883a1a15e695c972a')),(的ObjectId( '530b2ad783a1a15e695c970b'),的ObjectId( '530b804883a1a15e695c972a')),(的ObjectId( '530b2ad783a1a15e695c9711'),的ObjectId( '530b804883a1a15e695c972b')),(的ObjectId( '530b2ad783a1a15e695c971c'), ObjectId('530b804883a1a15e695c972b'))]

生产:enter image description here

例如,所有“根”节点因此没有接收到边的节点应该放在第一行,但它会将其中一些放在第二层。

一旦我尝试制作更大的图表,有 10 代,所有的层次结构都没有任何意义。

是否有任何参数或方法来指定节点的级别或层?在我提供的数据中,信息不存在,但我可以轻松生成它,问题是我不知道如何将该信息发送到 pygraphviz 或 networkx。

我也想使用这些数据来生成带有 twopi 的图形,就像这样一个 http://networkx.github.io/documentation/latest/examples/drawing/lanl_routes.html

其中图层以圆形方式表示。

最佳答案

使用属性 rank=same 的 Graphviz 子图。
例如

import networkx as nx
import pygraphviz as pgv # pygraphviz should be available

G = nx.DiGraph()
G.add_edge('a','aa')
G.add_edge('a','ab')
G.add_edge('a','bbc')
G.add_edge('b','ab')
G.add_edge('b','bb')
G.add_edge('c','bbc')
G.add_edge('bb','bba')
G.add_edge('bb','bbc')
A = nx.to_agraph(G)
one = A.add_subgraph(['a','b','c'],rank='same')
two = A.add_subgraph(['aa','ab','bb'],rank='same')
three = A.add_subgraph(['bba','bbc'],rank='same')
A.draw('example.png', prog='dot')

enter image description here

关于python - Pygraphviz/networkx 设置节点级别或层,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22207802/

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