[向下滚动一下,看看代码会产生什么样的输出]
编辑(2019 年 11 月 7 日)我已将其更精致的版本放入我一直在编写的包中:https://epidemicsonnetworks.readthedocs.io/en/latest/_modules/EoN/auxiliary.html#hierarchy_pos。这里的代码和那里的版本之间的主要区别在于,这里的代码为给定节点的所有子节点提供了相同的水平空间,而该链接后面的代码在决定分配多少空间时还考虑了节点有多少后代.
编辑(2019 年 1 月 19 日) 我更新了代码,使其更加健壮:它现在适用于有向图和无向图,无需任何修改,不再需要用户指定根,并且它在运行之前测试该图是一棵树(如果没有测试,它将具有无限递归 - 请参阅 user2479115 的答案以了解处理非树的方法)。
编辑(2018 年 8 月 27 日)如果您想创建一个节点显示为围绕根节点的环的图,则底部的代码显示了一个简单的修改来执行此操作
编辑(2017 年 9 月 17 日) 我相信 OP 遇到的 pygraphviz 问题现在应该得到解决。所以 pygraphviz 可能是比我下面得到的更好的解决方案。
这是一个定义位置的简单递归程序。递归发生在_hierarchy_pos,由hierarchy_pos 调用。 hierarcy_pos的主要作用是在进入递归之前做一些测试,确保图合适:
import networkx as nx
import random
def hierarchy_pos(G, root=None, width=1., vert_gap = 0.2, vert_loc = 0, xcenter = 0.5):
'''
From Joel's answer at https://stackoverflow.com/a/29597209/2966723.
Licensed under Creative Commons Attribution-Share Alike
If the graph is a tree this will return the positions to plot this in a
hierarchical layout.
G: the graph (must be a tree)
root: the root node of current branch
- if the tree is directed and this is not given,
the root will be found and used
- if the tree is directed and this is given, then
the positions will be just for the descendants of this node.
- if the tree is undirected and not given,
then a random choice will be used.
width: horizontal space allocated for this branch - avoids overlap with other branches
vert_gap: gap between levels of hierarchy
vert_loc: vertical location of root
xcenter: horizontal location of root
'''
if not nx.is_tree(G):
raise TypeError('cannot use hierarchy_pos on a graph that is not a tree')
if root is None:
if isinstance(G, nx.DiGraph):
root = next(iter(nx.topological_sort(G))) #allows back compatibility with nx version 1.11
else:
root = random.choice(list(G.nodes))
def _hierarchy_pos(G, root, width=1., vert_gap = 0.2, vert_loc = 0, xcenter = 0.5, pos = None, parent = None):
'''
see hierarchy_pos docstring for most arguments
pos: a dict saying where all nodes go if they have been assigned
parent: parent of this branch. - only affects it if non-directed
'''
if pos is None:
pos = {root:(xcenter,vert_loc)}
else:
pos[root] = (xcenter, vert_loc)
children = list(G.neighbors(root))
if not isinstance(G, nx.DiGraph) and parent is not None:
children.remove(parent)
if len(children)!=0:
dx = width/len(children)
nextx = xcenter - width/2 - dx/2
for child in children:
nextx += dx
pos = _hierarchy_pos(G,child, width = dx, vert_gap = vert_gap,
vert_loc = vert_loc-vert_gap, xcenter=nextx,
pos=pos, parent = root)
return pos
return _hierarchy_pos(G, root, width, vert_gap, vert_loc, xcenter)
以及一个示例用法:
import matplotlib.pyplot as plt
import networkx as nx
G=nx.Graph()
G.add_edges_from([(1,2), (1,3), (1,4), (2,5), (2,6), (2,7), (3,8), (3,9), (4,10),
(5,11), (5,12), (6,13)])
pos = hierarchy_pos(G,1)
nx.draw(G, pos=pos, with_labels=True)
plt.savefig('hierarchy.png')
理想情况下,这应该根据其下方的宽度重新调整水平分隔。我没有尝试,但这个版本可以:https://epidemicsonnetworks.readthedocs.io/en/latest/_modules/EoN/auxiliary.html#hierarchy_pos
径向膨胀
假设您希望情节看起来像:
代码如下:
pos = hierarchy_pos(G, 0, width = 2*math.pi, xcenter=0)
new_pos = {u:(r*math.cos(theta),r*math.sin(theta)) for u, (theta, r) in pos.items()}
nx.draw(G, pos=new_pos, node_size = 50)
nx.draw_networkx_nodes(G, pos=new_pos, nodelist = [0], node_color = 'blue', node_size = 200)
编辑 - 感谢 Deepak Saini 指出一个曾经出现在有向图中的错误