2017-09-15 12 views
0

私は{'human', 'machine'}という2つのタイプに属する小さな例のノードセットで作業しています。ノードの属性をノードの外側のディクショナリ形式でnetworkxのグラフのノードc、 e、jを下のグラフに示します。 (I辞書型グラフに属性を追加するために、MS Wordを使用。):ラベル付けネットワークxノード外のノード属性

import networkx as nx 
G = nx.Graph() 
G.add_nodes_from(['a', 'b', 'c', 'd', 'e', 'f', 'g'], type = 'machine') 
G.add_nodes_from(['h', 'i', 'j'], type = 'human') 
G.add_edges_from([('a', 'c'), ('a', 'b'), ('a', 'd'), ('a', 'f'), ('b', 'd'), ('b', 'e'), ('b', 'g'), ('c', 'f'), ('c', 'd'), ('d', 'f'), ('d', 'e'), ('d', 'g'), ('e', 'g'), ('f', 'g'), ('f', 'h'), ('g', 'h'), ('h', 'i'), ('i', 'j')]) 

def plot_graph(G, weight_name=None): 
    import matplotlib.pyplot as plt 

    plt.figure() 
    pos = nx.spring_layout(G) 
    edges = G.edges() 
    weights = None 

    if weight_name: 
     weights = [int(G[u][v][weight_name]) for u,v in edges] 
     labels = nx.get_edge_attributes(G,weight_name) 
     nx.draw_networkx_edge_labels(G,pos,edge_labels=labels) 
     nx.draw_networkx(G, pos, edges=edges, width=weights); 
    else: 
     nx.draw_networkx(G, pos, edges=edges); 

plot_graph(G, weight_name=None) 
plt.savefig('example.png') 
plt.show() 

しかし、ここでは問題である、

enter image description here

ベースプロットは、次のコードを使用して生成されます

nx.get_node_attributes()および関数は、ラベルに辞書キー(この例では 'type')を含めません(nx.get_edge_attributes()およびnx.draw_networkx_edge_labels()のみ) nx.get_node_attributes()nx.draw_networkx_labels()を使用する場合、元のノード名は属性値に置き換えられます。

ノード内にノード名を保持しながら、各ノードの外に辞書形式の属性にラベルを付ける別の方法があるのでしょうか?現在のコードをどのように変更すればよいですか?

答えて

1

キーを含まないnx.draw_networkx_labels()の問題は、dicts全体を値として表す文字列を保持する新しいdictを作成することで解決できます。

node_attrs = nx.get_node_attributes(G, 'type') 
custom_node_attrs = {} 
for node, attr in node_attrs.items(): 
    custom_node_attrs[node] = "{'type': '" + attr + "'}" 

ノード名と属性を描画について、あなたは属性を描画するために、通常、ノード名を描画するためにnx.draw()を使用し、nx.draw_networkx_labels()ことができます - ここでは、手動でノードの上または下にあることを属性の位置をずらすことができます。以下のブロックにおいて、posは、ノード位置を保持し、pos_attrsは、適切なノードの上に配置された属性位置を保持する。

pos_nodes = nx.spring_layout(G) 
pos_attrs = {} 
for node, coords in pos_nodes.items(): 
    pos_attrs[node] = (coords[0], coords[1] + 0.08) 

全例:

import networkx as nx 
import matplotlib.pyplot as plt 

G = nx.Graph() 
G.add_nodes_from(['a', 'b', 'c', 'd', 'e', 'f', 'g'], type = 'machine') 
G.add_nodes_from(['h', 'i', 'j'], type = 'human') 
G.add_edges_from([('a', 'c'), ('a', 'b'), ('a', 'd'), ('a', 'f'), ('b', 'd'), ('b', 'e'), ('b', 'g'), ('c', 'f'), ('c', 'd'), ('d', 'f'), ('d', 'e'), ('d', 'g'), ('e', 'g'), ('f', 'g'), ('f', 'h'), ('g', 'h'), ('h', 'i'), ('i', 'j')]) 

plt.figure() 
pos_nodes = nx.spring_layout(G) 
nx.draw(G, pos_nodes, with_labels=True) 

pos_attrs = {} 
for node, coords in pos_nodes.items(): 
    pos_attrs[node] = (coords[0], coords[1] + 0.08) 

node_attrs = nx.get_node_attributes(G, 'type') 
custom_node_attrs = {} 
for node, attr in node_attrs.items(): 
    custom_node_attrs[node] = "{'type': '" + attr + "'}" 

nx.draw_networkx_labels(G, pos_attrs, labels=custom_node_attrs) 
plt.show() 

出力:

enter image description here

最後のヒント:あなたは多くのエッジを持っているし、あなたのノード属性が読みにくい場合は、あなたが試すことができますエッジの色をより明るいグレーの色に設定します。

+1

これを明確にしてくれてありがとうございました。あなたの答えは私の問題を解決するのに役立ちます! –

関連する問題