Pygraphviz / networkx 设置节点级别或层
Posted
技术标签:
【中文标题】Pygraphviz / networkx 设置节点级别或层【英文标题】:Pygraphviz / networkx set node level or layer 【发布时间】:2014-04-08 02:29:58 【问题描述】:我有一个代表一种家谱树的数据集。每个节点有 2 个父节点(第一代除外,它们没有父节点)。对于给定的节点,其父节点可以来自任何上一代。例如,第 n 代中的节点可以在 n-1 中有一个父节点,在 n-5 中有另一个父节点。一个节点可以是其他几个节点的父节点。
所以基本上,对于每个节点,我都知道它的世代和父节点。
我试图表示此图,将同一代的节点保持在同一行中。每一代都有10个节点,除了第一个节点。
到目前为止,我正在尝试“点”布局。当我只输入两代时它做得很好,但是当我输入三代时,它“太聪明了”,并且以某种方式重新排列了节点。
例如以下数据,代表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('530b804883a 1a15e695c9726 ')),(的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'), 对象 I d( '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('530b2ad783a1a15e695c9 716 '),的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('530b2ad 783a1a15e695c9719 '),的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'))]
产生:
例如,所有“根”节点因此不接收边应放置在第一行,但它会将其中一些节点放置在第二层。
一旦我尝试制作更大的图表,包含 10 代,所有层次结构都毫无意义。
是否有任何参数或方法来指定节点的级别或层?在我提供的数据中,该信息不存在,但我可以轻松生成它,问题是我不知道如何将该信息发送到 pygraphviz 或 networkx。
我也想使用这些数据来生成带有 twopi 的图形,例如 http://networkx.github.io/documentation/latest/examples/drawing/lanl_routes.html
图层以圆形方式表示。
【问题讨论】:
【参考方案1】:使用属性 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')
【讨论】:
谢谢,这很好用。我看到的问题是“pygraphviz”完成的图比networkx更丑(即使networkx使用graphviz制作它)并且与边缘相比更难指定节点大小等内容 如果子图中只有部分节点共享相同的排名?【参考方案2】:自 2017 年起,to_agraph
函数不再在 nx.level
中公开。
现在你必须打电话给nx.nx_agraph.to_agraph()
【讨论】:
【参考方案3】:此链接将对您有所帮助。 rank_same.py
import graphviz
d = graphviz.Digraph(filename='rank_same.gv')
with d.subgraph() as s:
s.attr(rank='same')
s.node('A')
s.node('X')
d.node('C')
with d.subgraph() as s:
s.attr(rank='same')
s.node('B')
s.node('D')
s.node('Y')
d.edges(['AB', 'AC', 'CD', 'XY'])
d.view()
【讨论】:
以上是关于Pygraphviz / networkx 设置节点级别或层的主要内容,如果未能解决你的问题,请参考以下文章
以编程方式在 networkx 的 pygraphviz/dot 包装器中指定相同等级的节点