可以使用python 3从networkx获取层次图吗? [英] Can one get hierarchical graphs from networkx with python 3?

查看:134
本文介绍了可以使用python 3从networkx获取层次图吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 networkx. 显示我的类层次结构的树状图,我都正确地绘制了图,并显示了 fine .但是作为具有交叉边缘的圆形图,它是一个纯层次结构,看来我应该能够将其显示为树.

I am trying to display a tree graph of my class hierarchy using networkx. I have it all graphed correctly, and it displays fine. But as a circular graph with crossing edges, it is a pure hierarchy, and it seems I ought to be able to display it as a tree.

我对此进行了广泛的搜索,提供的每个解决方案都涉及使用 pygraphviz ...,但是 PyGraphviz不适用于Python 3(pygraphviz网站上的文档) .

I have googled this extensively, and every solution offered involves using pygraphviz... but PyGraphviz does not work with Python 3 (documentation from the pygraphviz site).

有人能够在Python 3中获得树形图显示吗?

Has anyone been able to get a tree graph display in Python 3?

推荐答案

[向下滚动以查看代码产生什么样的输出]

[scroll down a bit to see what kind of output the code produces]

编辑(2019年11月7日),我已经在编写的软件包中添加了一个更完善的版本:

edit (7 Nov 2019) I've put a more refined version of this into a package I've been writing: https://epidemicsonnetworks.readthedocs.io/en/latest/_modules/EoN/auxiliary.html#hierarchy_pos. The main difference between the code here and the version there is that the code here gives all children of a given node the same horizontal space, while the code following that link also considers how many descendants a node has when deciding how much space to allocate it.

编辑(2019年1月19日),我将代码更新为更加健壮:它现在可用于有向图和无向图,而无需进行任何修改,不再需要用户指定根,并且在运行之前测试该图是否为树(如果没有测试,它将具有无限递归-有关处理非树的方法,请参见user2479115的答案).

edit (19 Jan 2019) I have updated the code to be more robust: It now works for directed and undirected graphs without any modification, no longer requires the user to specify the root, and it tests that the graph is a tree before it runs (without the test it would have infinite recursion - see user2479115's answer for a way to handle non-trees).

编辑(2018年8月27日)如果您要创建一个图,其中节点在根节点周围显示为环,则底部的代码将对此进行简单的修改

edit (27 Aug 2018) If you want to create a plot with the nodes appearing as rings around the root node, the code right at the bottom shows a simple modification to do this

编辑(2017年9月17日),我相信现在应该解决pygraphviz带来的OP麻烦.因此,pygraphviz可能是比我在下面得到的更好的解决方案.

edit (17 Sept 2017) I believe the trouble with pygraphviz that OP was having should be fixed by now. So pygraphviz is likely to be a better solution that what I've got below.

这是一个简单的递归程序,用于定义位置.递归发生在_hierarchy_pos中,由hierarchy_pos调用. hierarcy_pos的主要作用是进行一些测试,以确保图形在进入递归之前是合适的:

Here is a simple recursive program to define the positions. The recursion happens in _hierarchy_pos, which is called by hierarchy_pos. The main role of hierarcy_pos is to do a bit of testing to make sure the graph is appropriate before entering the recursion:

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')

理想情况下,这应该根据水平分隔线下方的宽度重新调整水平分隔线的比例.我现在没有尝试.

Ideally this should rescale the horizontal separation based on how wide things will be beneath it. I'm not attempting that now.

径向扩展

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

Let's say you want the plot to look like:

这是代码:

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


edit - thanks to Deepak Saini for noting an error that used to appear in directed graphs

这篇关于可以使用python 3从networkx获取层次图吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆