Подтвердить что ты не робот

Можно ли получить иерархические графики из networkx с помощью python 3?

Я пытаюсь отобразить древовидную диаграмму моей иерархии классов, используя networkx.. Я все правильно рисую, и она отображается нормально. Но как круговой граф с пересекающимися ребрами, это чистая иерархия, и кажется, что я должен показать его как дерево.

Я искал это широко, и каждое предлагаемое решение включает в себя использование pygraphviz... но PyGraphviz не работает с Python 3 (документация с сайта pygraphviz).

Кто-нибудь смог получить отображение древовидного графика в Python 3?

4b9b3361

Ответ 1

edit (19 января 2019) Я обновил код, чтобы сделать его более устойчивым: теперь он работает для ориентированных и ненаправленных графов без каких-либо изменений, больше не требует от пользователя указывать корень, и он проверяет, что граф является деревом, прежде чем он запустится (без теста у него была бы бесконечная рекурсия - см. ответ user2479115 для способа обработки не-деревьев).

edit (27 августа 2018 г.) Если вы хотите создать график, в котором узлы отображаются в виде колец вокруг корневого узла, код справа внизу показывает простую модификацию для этого

edit (17 сентября 2017 г.) Я считаю, что проблема с pygraphviz, которую имел OP, должна быть исправлена к настоящему времени. Так что 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 answer at https://stackoverflow.com/a/29597209/2966723 

    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 за замечание ошибки, которая раньше появлялась в ориентированных графах

Ответ 2

Я немного изменился, так что он не будет бесконечно рекурсивно.

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

Вот решение для больших деревьев. Это модификация рекурсивного подхода Джоэла, который равномерно распределяет узлы на каждом уровне.

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

Ответ 4

Самый простой способ получить красивое отображение графов деревьев в Python 2 или 3 без PyGraphviz - это использовать 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. Этого можно избежать, пропустив словари.

Ответ 5

Для ориентированного графа. Поскольку соседи (x) включают только succesors (x), поэтому вам нужно удалить строки:

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

Кроме того, лучшим вариантом будет следующее:

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