问题描述
我有一个代表族谱树的数据集.每个节点有2个父节点(第一代除外,它们没有父节点).对于给定的节点,其父节点可以来自任何上一代.例如,第n代中的一个节点在n-1中可以有一个父节点,而在n-5中可以有另一个父节点.一个节点可以是其他几个节点的父节点.
I have a dataset that represents a kind of genealogy tree. Each node has 2 parents (except first generation, they have no parents). For a given node, its parents can be from any previous generation. For example a node in generation n, can have a parent in n-1, and another parent in n-5. A node can be parent of several other nodes.
所以基本上,对于每个节点,我都知道节点的生成及其父节点.
So basically, for every node I know its generation and its parents.
我试图表示此图,以保持同一行中同一代的节点.除第一个节点外,每一代都有10个节点.
I am trying to represent this graph keeping the nodes from the same generation in the same line. Each generation has 10 nodes, except the first one.
到目前为止,我正在尝试点"式布局.当我只输入两代时,它做得很好,但是当我输入三代时,它太聪明了",并以某种方式重新排列了节点.
So far I am trying the "dot" layout. When I only input two generations it does a good job, but when I input 3 generations, it is "too smart", and rearranges the nodes in some way.
例如,以下数据代表3代:
For example, the following data, which represents 3 generations:
[(ObjectId('530b2ad783a1a15e695c9721'),ObjectId('530b804883a1a15e695c972b')),(ObjectId('530b2ad783a1a15e695c971c'),ObjectId('530b804883a1a15e695cccbe)) ),(ObjectId('530b2ad783a1a15e695c97a'),ObjectId('530b804883a1a15e695c9727')),(ObjectId('530b2ad783a1a15e695c970b'),ObjectId('530b804883a1a15e695c97e24e),ObjectId('530b2a)15,a1a1a1a15a3e7e9e15,a9e1'a1af1a3e5,a7e15,a1e1,a5a3,a3d,a5d(a)(1) (ObjectId('530b2ad783a1a15e695c9714'),ObjectId('530b804883a1a15e695c9729')),(ObjectId('530b2ad783a1a15e695c970e'),ObjectId('530b804883a1a15e695c9729')),(ObjectId('530b2ad783a1a1a1a15a1a15e695c97695') ('530b2ad783a1a15e695c970e'),ObjectId('530b804883a1a15e695c9723')),(ObjectId('530b2ad783a1a15e695c9728'),ObjectId('530b804883a1a15e695c9728')),(ObjectId('530b2ad783a'93a695(c)695a(695e)(c) 530b2ad783a1a15e695c9714'),ObjectId('530b804883a 1a15e695c9726')),(ObjectId('530b2ad783a1a15e695c9713'),ObjectId('530b804883a1a15e695c9726')),(ObjectId('530b2ad783a1a15e695c9716'),ObjectId('530b804883a1a15e695c972972( )),(ObjectId('530b2ad783a1a15e695c97d'),ObjectId('530b804883a1a15e695c9725')),(ObjectId('530b2ad783a1a15e695c9715'),ObjectId('530b804883a1a15e695c97e25')),(ObjectId('530b804883a1a15e695c9725')),(ObjectId(1530) ,(ObjectId('530b2ad783a1a15e695c9713'),ObjectId('530ba27c83a1a15e695c972d')),(ObjectId('530b804883a1a15e695c97e'),ObjectId('530ba27c83a1a15e695c972e'),(ObjectId('530a2a),(530) ObjectId('530b804883a1a15e695c9727'),ObjectId('530ba27c83a1a15e695c972f')),(ObjectId('530b2ad783a1a15e695c9709'),ObjectId('530ba27c83a1a15e695c972f')),(ObjectId('530a1aa(79a1a), '530b2ad783a1a15e695c9709'),ObjectI d('530ba27c83a1a15e695c9730')),(ObjectId('530b2ad783a1a15e695c9713'),ObjectId('530ba27c83a1a15e695c9731')),(ObjectId('530b804883a1a15e695c9725'),ObjectId('530ba27c83a1e1a1a1e1a1a1e3a1e7a7a1e7a1e7a7e7a7a7e1a1e1b1e1b3b3b3b3b2b3b1b1b1b1b1b3b使用者'530ba27c83a1a15e695c9732')),(ObjectId('530b2ad783a1a15e695c970b'),ObjectId('530ba27c83a1a15e695c9732')),(ObjectId('530b2ad783a1a15e695c9711'),ObjectId('530ba27c83a1cc1a1ec1c1c1a1e1c3c7a1e1c3c7a1e1c3c7a1e1c3a7e1b1e7c3a1b1e7c5a7b1e7b2b3b3b使用者职位')),(ObjectId('530b804883a1a15e695c972b'),ObjectId('530ba27c83a1a15e695c9734')),(ObjectId('530b2ad783a1a15e695c9713'),ObjectId('530ba27c83a1a15e695c973e'5a)(1) ),(ObjectId('530b804883a1a15e695c9724'),ObjectId('530ba27c83a1a15e695c9735')),(ObjectId('530b2ad783a1a15e695c9713'),ObjectId('530ba27c83a1a15e695cc976')),(ObjectId('530a27'15)15)(15) (ObjectId('530b2ad783a1a15e695c9 716'),ObjectId('530b804883a1a15e695c9722')),(ObjectId('530b2ad783a1a15e695c972'),ObjectId('530b804883a1a15e695c9722')),(ObjectId('530b2ad783a1a15e695cc9751'),ObjectId('530b2a1a'a1a1a1a1a1a1e1e1e1e1e1e1e1e1e1e5c3e1e5e5e7b3bfbfbfb2b2b3b3b3b3b3b使用者的对象(3d) ),ObjectId('530b804883a1a15e695c9723')),(ObjectId('530b2ad783a1a15e695c970b'),ObjectId('530b804883a1a15e695c9724')),(ObjectId('530b2ad783a1a15e695cc970f'),ObjectId('530b8041a15e695a2e, ObjectId('530b804883a1a15e695c9725')),(ObjectId('530b2ad783a1a15e695c9725'),ObjectId('530b804883a1a15e695c9725')),(ObjectId('530b2ad783a1a15e695c97Id' '530b804883a1a15e695c9726')),(ObjectId('530b2ad783a1a15e695c9709'),ObjectId('530b804883a1a15e695c9727')),(ObjectId('530b2ad783a1a15e695c970a'),ObjectId('530b804883a1a9a97a97a97a97a97a97a97a97a97a ')),(ObjectId('530b2ad 783a1a15e695c9719'),ObjectId('530b804883a1a15e695c9728')),(ObjectId('530b2ad783a1a15e695c9714'),ObjectId('530b804883a1a15e695c9729')),(ObjectId('530b2ad783a1a15e695c17e9a15e695e ),ObjectId('530b804883a1a15e695c972a')),(ObjectId('530b2ad783a1a15e695c970a'),ObjectId('530b804883a1a15e695c972a')),(ObjectId('530b2ad783a1a15e695cc1937'),ObjectId('530b804a15a695e2a1), ObjectId('530b804883a1a15e695c972b'))]
[(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('530b804883a1a15e695c9726')), (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('530b2ad783a1a15e695c9719'), 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'))]
产生:
例如,所有属于根"节点且因此未获得边缘的节点都应放置在第一行上,但会将其中一些放置在第二层上.
All the nodes that are "root" and therefore don't receive an edge should be placed on the first line, for example, but it is placing some of them on the second level.
一旦我尝试制作更大的图形(具有10代),所有的层次结构都毫无意义.
Once I try to make bigger graphs, with 10 generations, all the hierarchy makes no sense at all.
是否有任何参数或方法可以指定节点的级别或层?在我提供的信息中没有该信息,但是我可以很容易地生成它,问题是我不知道如何将该信息发送到pygraphviz或networkx.
is there any parameter or way to specify the level or layer of a node? In the data I provided that info is not there, but I can easily produce it, the problem is that I don't know how to send that info to pygraphviz or networkx.
我也想使用此数据来生成具有doublepi的图形,例如 http://networkx.github.io/documentation/latest/examples/drawing/lanl_routes.html
I also want to use this data to produce graphs with twopi like this one http://networkx.github.io/documentation/latest/examples/drawing/lanl_routes.html
其中的图层以圆形方式表示.
where the layers are represented on a circular way.
推荐答案
使用属性rank = same的Graphviz子图.例如
Use a Graphviz subgraph with attribute rank=same.e.g.
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')
这篇关于pygraphviz/networkx设置节点级别或层的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!