可以使用python 3从networkx获取分层图吗?

时间:2015-04-12 06:06:57

标签: python networkx pygraphviz

我正在尝试使用 networkx. 显示我的类层次结构的树形图。我已将其全部绘制正确,并显示 fine 。但是作为一个带有交叉边的圆形图,它是一个纯粹的层次结构,似乎我应该能够将它显示为树。

我已经广泛搜索了这个,并且提供的每个解决方案都涉及使用 pygraphviz ...但 PyGraphviz不适用于Python 3(来自pygraphviz网站的文档) )

有没有人能够在Python 3中显示树形图?

6 个答案:

答案 0 :(得分:29)

编辑(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')

enter image description here

理想情况下,这应根据物体下方的宽度重新调整水平间距。我现在不是在尝试。

径向扩张

假设您希望情节看起来像:

enter image description here

以下是代码:

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注意到曾经出现在有向图中的错误

答案 1 :(得分:8)

这是大树的解决方案。它是对Joel递归方法的修改,它在每个级别均匀地分隔节点。

def hierarchy_pos(G, root, levels=None, width=1., height=1.):
    '''If there is a cycle that is reachable from root, then this will see infinite recursion.
       G: the graph
       root: the root node
       levels: a dictionary
               key: level number (starting from 0)
               value: number of nodes in this level
       width: horizontal space allocated for drawing
       height: vertical space allocated for drawing'''
    TOTAL = "total"
    CURRENT = "current"
    def make_levels(levels, node=root, currentLevel=0, parent=None):
        """Compute the number of nodes for each level
        """
        if not currentLevel in levels:
            levels[currentLevel] = {TOTAL : 0, CURRENT : 0}
        levels[currentLevel][TOTAL] += 1
        neighbors = G.neighbors(node)
        for neighbor in neighbors:
            if not neighbor == parent:
                levels =  make_levels(levels, neighbor, currentLevel + 1, node)
        return levels

    def make_pos(pos, node=root, currentLevel=0, parent=None, vert_loc=0):
        dx = 1/levels[currentLevel][TOTAL]
        left = dx/2
        pos[node] = ((left + dx*levels[currentLevel][CURRENT])*width, vert_loc)
        levels[currentLevel][CURRENT] += 1
        neighbors = G.neighbors(node)
        for neighbor in neighbors:
            if not neighbor == parent:
                pos = make_pos(pos, neighbor, currentLevel + 1, node, vert_loc-vert_gap)
        return pos
    if levels is None:
        levels = make_levels({})
    else:
        levels = {l:{TOTAL: levels[l], CURRENT:0} for l in levels}
    vert_gap = height / (max([l for l in levels])+1)
    return make_pos({})

乔尔的例子如下: enter image description here

这是一个更复杂的图形(使用图表渲染):enter image description here

答案 2 :(得分:7)

我略微修改,以至于它无法无限递归。

import networkx as nx

def hierarchy_pos(G, root, width=1., vert_gap = 0.2, vert_loc = 0, xcenter = 0.5 ):
    '''If there is a cycle that is reachable from root, then result will not be a hierarchy.

       G: the graph
       root: the root node of current branch
       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
    '''

    def h_recur(G, root, width=1., vert_gap = 0.2, vert_loc = 0, xcenter = 0.5, 
                  pos = None, parent = None, parsed = [] ):
        if(root not in parsed):
            parsed.append(root)
            if pos == None:
                pos = {root:(xcenter,vert_loc)}
            else:
                pos[root] = (xcenter, vert_loc)
            neighbors = G.neighbors(root)
            if parent != None:
                neighbors.remove(parent)
            if len(neighbors)!=0:
                dx = width/len(neighbors) 
                nextx = xcenter - width/2 - dx/2
                for neighbor in neighbors:
                    nextx += dx
                    pos = h_recur(G,neighbor, width = dx, vert_gap = vert_gap, 
                                        vert_loc = vert_loc-vert_gap, xcenter=nextx, pos=pos, 
                                        parent = root, parsed = parsed)
        return pos

    return h_recur(G, root, width=1., vert_gap = 0.2, vert_loc = 0, xcenter = 0.5)

答案 3 :(得分:5)

在没有PyGraphviz的Python 2或3中获得漂亮的树形图显示的最简单方法是使用PyDot(https://pypi.python.org/pypi/pydot)。 PyGraphviz为整个Graphviz提供了一个接口,而PyDot只为Graphviz的Dot工具提供了一个接口,如果你所追求的是一个分层图形/树,那么它就是你唯一需要的工具。如果要在NetworkX而不是PyDot中创建图形,可以使用NetworkX导出PyDot图形,如下所示:

import networkx as nx

g=nx.DiGraph()
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)])
p=nx.drawing.nx_pydot.to_pydot(g)
p.write_png('example.png')

请注意,需要安装Graphviz和PyDot才能使上述内容正常工作。

enter image description here

警告:使用PyDot绘制带有从NetworkX导出的节点属性词典的图形时遇到问题 - 有时字典似乎导出字符串中缺少引号,导致write方法崩溃。这可以通过省略字典来避免。

答案 4 :(得分:2)

对于有向图,由于邻居(x)只包含成功者(x),所以你必须删除这些行:

if parent != None:
        neighbors.remove(parent)

此外,更好的选择是:

pos=nx.graphviz_layout(G,prog='dot')

答案 5 :(得分:0)

我将grandalf用于不使用graphviz和pygraphviz的纯Python解决方案。

此外,这种可视化类型称为layered graph drawingSugiyama-style graph drawing,它们可以显示多种图形,包括非树。

HTMLMediaElement