2017-12-13 1 views
0

저는 사람들이 networkx 모듈을 사용하여 네트워크 그래프를 그려보고자합니다. 그러나 기대하지 않았던 결과를 얻고 있으며 모듈 문제가 있다면 스스로에게 묻기 시작했습니다. (draw_graph 코드가 https://www.udacity.com/wiki/creating-network-graphs-with-python에 본사를두고있다) 나는 draw_graph 내부에 전달되는 그 무엇을 볼 수있는 인쇄에서Networkx 버그? 색깔이 잘못 위치했습니다.

def plotGraph(self): 
    conn = [] 
    nodeLabel = {} 

    for node_idx in self.operatorNodes: 
     print("i = ", node_idx) 
     print(self.node[node_idx].childs) 
     for child in self.node[node_idx].childs: 
      conn.append((child.idx, node_idx)) 

    for i in range(self.nn): 
     nodeLabel[i] = str(i) + ": " + self.node[i].opString 

    node_color = ['blue'] * self.nn 
    #for i in range(self.nOutputs): 
    # node_color[i] = 'red' 

    node_color[0] = 'red' 

    print('Graph Conn = ', conn) 
    print('Graph Color = ', node_color) 
    # you may name your edge labels 
    labels = map(chr, range(65, 65 + len(conn))) 
    print('nodeLabel = ', nodeLabel) 

    draw_graph(conn, nodeLabel, node_color=node_color, labels=labels) 

됩니다 :

나는이 클래스 내부의 코드가

Graph Conn = [(2, 0), (3, 0), (4, 1), (5, 1), (6, 2), (7, 2), (8, 5), (9, 5)] 
Graph Color = ['red', 'blue', 'blue', 'blue', 'blue', 'blue', 'blue', 'blue', 'blue', 'blue'] 
nodeLabel = {0: '0: mul', 1: '1: mul', 2: '2: mul', 3: '3: cte', 4: '4: cte', 5: '5: sum', 6: '6: cte', 7: '7: cte', 8: '8: cte', 9: '9: cte'} 

Yet the plot is the following

draw_graph 코드는 다음과 같습니다

def draw_graph(graph, nodeLabel, node_color, labels=None, graph_layout='shell', 
       node_size=1600, node_alpha=0.3, 
       node_text_size=12, 
       edge_color='blue', edge_alpha=0.3, edge_tickness=1, 
       edge_text_pos=0.3, 
       text_font='sans-serif'): 

    # create networkx graph 
    G=nx.DiGraph() 

    # add edges 
    for edge in graph: 
     G.add_edge(edge[0], edge[1]) 

    # these are different layouts for the network you may try 
    # shell seems to work best 
    if graph_layout == 'spring': 
     graph_pos = nx.spring_layout(G) 
    elif graph_layout == 'spectral': 
     graph_pos = nx.spectral_layout(G) 
    elif graph_layout == 'random': 
     graph_pos = nx.random_layout(G) 
    else: 
     graph_pos = nx.shell_layout(G) 

    # draw graph 
    nx.draw_networkx_edges(G, graph_pos, width=edge_tickness, alpha=edge_alpha, edge_color=edge_color) 
    nx.draw_networkx_labels(G, graph_pos, labels=nodeLabel, font_size=node_text_size, font_family=text_font) 

    if labels is None: 
     labels = range(len(graph)) 

    edge_labels = dict(zip(graph, labels)) 
    nx.draw_networkx_edge_labels(G, graph_pos, edge_labels=edge_labels, label_pos=edge_text_pos) 

    nx.draw(G, graph_pos, node_size=node_size, alpha=node_alpha, node_color=node_color) 

그래프의 색깔이 0이고 붉은 색이 파란색이지만, 플롯이 세 번째 노드에 놓여 있습니다! 노드 1에 액세스 할 수있는 방법이 없습니다. 분명히 노드가 잘못 배치되었습니다! 노드 색상은 [2, 0, 3, 4, 5, ...] 위치에 배치됩니다.

+0

일관성이 있지만 'edge_tickness'는 아마도'edge_thickness' 여야합니다 – Joel

답변

0

nx.draw을 사용하고 (선택 사항) 색상 목록을 전달하면 (선택 사항) nodelist과 같은 순서로 해당 색상이 노드에 할당됩니다. 그러나 nodelist을 정의하지 않았습니다. 따라서 어떤 주문이든지 G.nodes()에서 나온 것으로 기본 설정됩니다.

networkx 그래프의 기본 데이터 구조는 사전이므로 cannot count on the nodes to have any specified order이라는 사실을 처리해야합니다.

nodelist을 원하는 순서로 nx.draw 명령에 전달하십시오.

+0

감사합니다. Joel, 그건 분명히 트릭입니다! –