2012-12-11 3 views
9

거리 매트릭스에서 그래프를 그리는 프로그램을 작성하고 있습니다. 잘 작동합니다. 이제 특정 노드와 특정 가장자리가 내 선택의 특정 색으로되기를 바랍니다. 어떻게해야합니까?Networkx 및 Graphviz의 특정 노드 색상 지정

이 프로그램은 파이썬에 있고 당신이 할 graphviz를를 사용하고 있기 때문에 Networkx과 graphviz를

import networkx as nx 
import numpy as np 
import pickle 
from random import randint 

p_file = open('pickles/distance') 
Dist = pickle.load(p_file) 
p_file.close() 
p_file = open('pickles/names') 
Names = pickle.load(p_file) 
p_file.close() 

dt = [('len', float)] 
A = np.array(Dist)*5 
A = A.view(dt) 

G = nx.from_numpy_matrix(A) 
G = nx.relabel_nodes(G, dict(zip(range(len(G.nodes())),Names)))  

G = nx.to_agraph(G) 
G.node_attr.update(ndcolor="red", node="DC", style="filled") 
G.edge_attr.update(color="none") 
G.draw('P1.png', format='png', prog='neato') 

답변

14

을 사용하는 당신은 graphviz를 이해 속성을 사용할 필요 그리기. 당신이 내 코드에서, 나는 거리 매트릭스에서 노드와 엣지를 추가하고 볼 수 있듯이 참조

import networkx as nx 
G = nx.Graph() 

G.add_node(1,color='red',style='filled',fillcolor='blue',shape='square') 
G.add_node(2,color='blue',style='filled') 
G.add_edge(1,2,color='green') 
G.node[2]['shape']='circle' 
G.node[2]['fillcolor']='red' 

A = nx.to_agraph(G) 
A.layout() 
A.draw('color.png') 
print A.to_string() 

http://www.graphviz.org/content/attrs

strict graph { 
    graph [bb="0,0,107.21,46.639"]; 
    node [label="\N"]; 
    1 [color=red, 
     fillcolor=blue, 
     height=0.5, 
     pos="18,28.639", 
     shape=square, 
     style=filled, 
     width=0.5]; 
    2 [color=blue, 
     fillcolor=red, 
     height=0.5, 
     pos="89.21,18", 
     shape=circle, 
     style=filled, 
     width=0.5]; 
    1 -- 2 [color=green, 
     pos="36.338,25.899 47.053,24.298 60.519,22.286 71.18,20.694"]; 
} 

enter image description here

+1

를 제공합니다. 언급 한 방법은이 경우 적용 할 수 없습니다. 다른 방법을 아십니까? – Anirudh

+1

그래프 G를 생성 한 후에 속성을 추가 할 수 있습니다. G. 노드 [nodename] [ 'color'] = 'red'등을 사용하십시오. – Aric

+1

오! 고마워요, 아릭. 다른 사람들에게도 도움이 될 수 있도록 답변에 추가 할 수 있습니까? – Anirudh