2017-09-15 29 views
0

{'human', 'machine'}의 두 가지 유형에 속하는 작은 예제 노드 세트에서 작업 중이며 networkx 그래프의 각 노드 외부에 사전 형태로 노드 속성을 레이블로 지정하려고합니다 (예 : 노드 c, e, j를 선택하십시오. (I 사전 형 그래프에 속성을 추가 할 MS 워드를 사용했습니다.) :레이블링 네트워크 x 노드 외부의 노드 속성

enter image description here

기본 줄거리는 다음과 같은 코드를 사용하여 생성됩니다

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

을하지만, 여기에 문제가있다,

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()의 문제는 전체 딕트를 값으로 나타내는 문자열을 보유 할 새 딕트를 작성하여 해결할 수 있습니다.

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

답변을 주셔서 감사합니다. 문제가 해결되면 도움이됩니다. –