2013-05-07 110 views
0

我有如下: networkx: change node color in draw_circularnetworkx遭遇節點沒有位置

畫一個PIC有兩種色彩的節點。

我有這樣一些數據:

4556 5092 0.7000 
4556 4785 0.7500 
4556 5397 0.7000 
4556 5139 0.7500 
4556 5937 0.8333 
4556 6220 0.7000 
4556 5139 0.7500 
4556 6220 0.7063 
4559 4563 0.7500 
4559 4770 0.7500 
4559 4837 0.7500 
4559 5640 0.7500 
4559 4563 0.7500 
4559 4770 0.7500 
4559 4837 0.7500 
4559 5640 0.7500 
4561 4607 1.0000 
4561 4600 0.7500 
4561 4562 0.7500 
4561 5090 0.7500 
4561 5197 1.0000 
4561 5182 0.7500 
4561 5937 0.7500 
4561 6143 0.7500 
4561 5632 1.0000 
4561 5870 1.0000 
4561 6220 0.6756 
4561 6143 0.7500 
4561 6220 0.6750 
4562 4607 0.7500 
4562 5197 0.7500 
4562 5632 0.7500 
4562 5870 0.7500 
4562 6220 0.6656 
4564 4837 0.7500 
4564 4837 0.7500 
4566 5805 0.8750 
4566 5981 0.6729 
4571 4919 0.7000 
4571 6004 0.7500 
4571 6089 0.7000 
4571 6044 0.7500 
4571 6041 0.7000 
4571 5863 0.7500 
4571 6089 0.6398 

前兩個科拉姆是節點,而第三個是邊緣的長度。

我的代碼是:

import matplotlib.pyplot as plt 
import networkx as nx 
G=nx.Graph() 
filedata1 = open("1.txt",'r') 
filedata2 = open("2.txt",'r') 
data1_nodes = set() 
data2_nodes = set() 

for line in filedata2: 
    #print line 
    datas = line.split() 
    data2_nodes.add(datas[0]) 
    data2_nodes.add(datas[1]) 
    #print datas 
    #G.add_node(int(datas[0])) 
    #G.add_node(int(datas[1])) 
    G.add_edge(int(datas[0]),int(datas[1]),length=float(datas[2])) 
    #G.add_edge(datas[0],datas[1],length=datas[2]) 

for line in filedata1: 
    #print line 
    datas = line.split() 
    data1_nodes.add(datas[0]) 
    data1_nodes.add(datas[1]) 
    G.add_edge(int(datas[0]),int(datas[1]),length=float(datas[2])) 

data1_list = list(data1_nodes) 
data2_list = list(data2_nodes) 
#nx.draw(G,with_lables=False,node_size=100) 
print 'i have ' ,G.number_of_nodes() 
#print G.nodes() 
pos=nx.spring_layout(G) 
#pos=nx.graphviz_layout(G) 
print pos 
print len(pos) 
print 4789 in pos 
nx.draw_networkx_nodes(G,pos,nodelist=data1_list,node_size=20,node_color='r') 
nx.draw_networkx_nodes(G,pos,nodelist=data2_list,node_size=20,node_color='b') 
plt.axis('off') 
plt.savefig("data.png") 
plt.show() 

的1.txt是:https://gist.github.com/young001/5531613 2.txt是:https://gist.github.com/young001/5531619

的回報是:

... 4.92, 483.05), 6090: (170.5, 1658.1), 6097: (240.99, 307.32), 6098: (486.58, 694.79), 6103: (1583.7, 107.5), 6104: (427.04, 394.65), 6106: (241.71, 1647.5), 6110: (886.42, 783.84), 6111: (1276.5, 1102.8), 6112: (1197.8, 1077.6), 6113: (1693.7, 1229.5), 6115: (986.75, 358.81), 6116: (109.71, 1317.5), 6118: (1179.3, 1208.9), 6119: (1182.6, 1013.3), 6122: (1336.5, 1581.2), 6125: (1184.4, 1383.5), 6128: (615.71, 85.5), 6131: (1147.7, 1198.8), 6133: (1286.3, 690.69), 6134: (1205.4, 684.59), 6136: (1148.7, 1188.5), 6139: (1195.1, 611.44), 6141: (1234.3, 840.18), 6143: (612.13, 1060.3)} 
736 
True 
Traceback (most recent call last): 
    File "dataplot.py", line 37, in <module> 
    nx.draw_networkx_nodes(G,pos,nodelist=data1_list,node_size=20,node_color='r') 
    File "/usr/local/lib/python2.7/dist-packages/networkx/drawing/nx_pylab.py", line 366, in draw_networkx_nodes 
    raise nx.NetworkXError('Node %s has no position.'%e) 
networkx.exception.NetworkXError: Node '4789' has no position. 

爲什麼4789是存在的,但它仍然不告訴?

回答

3

你在混合整數和字符串。嘗試

data2_nodes.add(int(datas[0])) 
data2_nodes.add(int(datas[1])) 
+0

是的,你是對的,thx很多aric – young001 2013-05-07 12:27:14