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

前端之家收集整理的这篇文章主要介绍了python – Pygraphviz / networkx设置节点级别或层前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个代表一种家谱树的数据集.每个节点有2个父母(第一代除外,他们没有父母).对于给定节点,其父节点可以来自任何前一代节点.例如,生成n中的节点可以具有n-1中的父节点和n-5中的另一个父节点.节点可以是其他几个节点的父节点.

基本上,对于每个节点,我都知道它的生成及其父节点.

我试图表示这个图表保持同一行中的同一代的节点.每一代都有10个节点,第一代除外.

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

例如,以下数据代表3代:

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

生产:

例如,所有“root”且因此不接收边缘的节点应放在第一行,但它将其中一些放在第二层.

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

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

我也想用这个数据来生成像这样的一个像这样的两个图形的图形

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

解决方法

使用属性rank = same的Graphviz子图. @H_404_28@例如
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')
原文链接:https://www.f2er.com/python/185755.html

猜你在找的Python相关文章