2017-12-13 7 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の位置のグラフの色は赤で残りは青でなければなりませんが、プロットは3番目のノードに配置されています。私はノード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、それは間違いなくトリックでした! –

関連する問題