2017-03-01 268 views
0

學習通過教程繪製蟒蛇圖: https://plot.ly/ipython-notebooks/network-graphs/網絡圖與Plotly

由於Nodes可以工作數量的唯一形式(做正確的,如果錯了)有,我已經取代我的節點值像G2dd2b14820721251並做反向映射到後來圖形

import plotly.plotly as py 
from plotly.graph_objs import * 
import networkx as nx 

node_to_nmbr_dict = { 
    'G2dd2b1482072125': 1, 
    'G2dd2b1482072126': 2, 
    'G2dd2b1482072127': 3 
} 
nmbr_to_node_dict = { 
    1: 'G2dd2b1482072125', 
    2: 'G2dd2b1482072126', 
    3: 'G2dd2b1482072127' 
} 

# create Graph object, with nodes = len(nmbr_to_node_dict), (i think!!) 
G=nx.random_geometric_graph(len(nmbr_to_node_dict),1) 

edge_list = [(1, 2), (2, 3), (3, 1)] 

# Remove default edges created automatically 
G.remove_edges_from(G.edges()) 

#add your own edges 
G.add_edges_from(edge_list) 

# Store position as node attribute data for random_geometric_graph and find node near center (0.5, 0.5) 
pos=nx.get_node_attributes(G,'pos') 

dmin=1 
ncenter=0 
for n in pos: 
    x,y=pos[n] 
    d=(x-0.5)**2+(y-0.5)**2 
    if d<dmin: 
     ncenter=n 
     dmin=d 

     p=nx.single_source_shortest_path_length(G,ncenter) 

# Add edges as disconnected lines in a single trace and nodes as a scatter trace   
edge_trace = Scatter(
    x=[], 
    y=[], 
    line=Line(width=0.5,color='#888'), 
    hoverinfo='none', 
    mode='lines') 

for edge in G.edges(): 
    x0, y0 = G.node[edge[0]]['pos'] 
    x1, y1 = G.node[edge[1]]['pos'] # <----- This here throws keys error 
    edge_trace['x'] += [x0, x1, None] 
    edge_trace['y'] += [y0, y1, None] 

KeyError    Traceback (most recent call last) 
<ipython-input-96-077055af3467> in <module>() 
     1 for edge in G.edges(): 
     2  x0, y0 = G.node[edge[0]]['pos'] 
----> 3  x1, y1 = G.node[edge[1]]['pos'] 
     4  edge_trace['x'] += [x0, x1, None] 
     5  edge_trace['y'] += [y0, y1, None] 

KeyError: 'pos'   

點使用我在上面提到的教程完全相同的要求,node_info有我G2dd2b1482072125值已被反向映射的數字在字典中node_to_nmbr_dict

有人可以指出我在錯誤地繪製圖表的位置嗎?


Termnial轉儲:

In [108]: for i in G.nodes(): 
    ...:  print "G.node : ", G.node 
    ...:  print "G.node[i] : ", G.node[i] 
    ...:  print "\n" 
    ...: 
G.node : {0: {'pos': [0.5509883914114821, 0.2750348146445808]}, 1: {'pos': [0.07961147691471337, 0.6834184588841679]}, 2: {'pos': [0.8659145315498231, 0.4056583698428097]}, 3: {}} 
G.node[i] : {'pos': [0.5509883914114821, 0.2750348146445808]} 


G.node : {0: {'pos': [0.5509883914114821, 0.2750348146445808]}, 1: {'pos': [0.07961147691471337, 0.6834184588841679]}, 2: {'pos': [0.8659145315498231, 0.4056583698428097]}, 3: {}} 
G.node[i] : {'pos': [0.07961147691471337, 0.6834184588841679]} 


G.node : {0: {'pos': [0.5509883914114821, 0.2750348146445808]}, 1: {'pos': [0.07961147691471337, 0.6834184588841679]}, 2: {'pos': [0.8659145315498231, 0.4056583698428097]}, 3: {}} 
G.node[i] : {'pos': [0.8659145315498231, 0.4056583698428097]} 


G.node : {0: {'pos': [0.5509883914114821, 0.2750348146445808]}, 1: {'pos': [0.07961147691471337, 0.6834184588841679]}, 2: {'pos': [0.8659145315498231, 0.4056583698428097]}, 3: {}} 
G.node[i] : {} 
+0

你是怎麼創建你的圖'G'的?或者更好的是,你可以發佈'G.node [0]'的輸出嗎? –

+1

對不起..我的壞....更新的問題! – NoobEditor

回答

1

您由於nx.random_geometric_graph編號從0開始 節點的 「減一」 的錯誤,而你edge_list(顯然)編號的節點從1開始。

例如,

In [80]: G = nx.random_geometric_graph(3, 1) 

In [81]: G.nodes() 
Out[81]: [0, 1, 2] 

到目前爲止,這麼好。每個節點都有一個pos

In [82]: G.nodes(data=True) 
Out[82]: 
[(0, {'pos': [0.3530839410903017, 0.6066802744128065]}), 
(1, {'pos': [0.8770904947229078, 0.642494748842952]}), 
(2, {'pos': [0.286083277031809, 0.2958147129607025]})] 

In [83]: edge_list = [(1, 2), (2, 3), (3, 1)] 

In [84]: G.remove_edges_from(G.edges()) 

In [85]: G.add_edges_from(edge_list) 

糟糕!現在,一個新的節點3已經出臺沒有pos

In [86]: G.nodes(data=True) 
Out[86]: 
[(0, {'pos': [0.3530839410903017, 0.6066802744128065]}), 
(1, {'pos': [0.8770904947229078, 0.642494748842952]}), 
(2, {'pos': [0.286083277031809, 0.2958147129607025]}), 
(3, {})] 

因此,G.node[edge[1]]['pos']可以引發KeyError時edge[1]爲3


你的代碼可以通過從每個節點的值中減去1是固定在edge_list(因此重新編號的節點從0開始):

edge_list = [(0, 1), (1, 2), (2, 0)] 
+0

真棒....很少有人迴應,當我伸出在SO上,這解決了我的問題。最後一個問題,'py.iplot(fig,include_plotlyjs = False,output_type ='div')'returns' ',我怎樣才能把它帶到HTML? – NoobEditor

+0

對不起,我並不熟悉。雖然看起來很有趣,必須學習一段時間。 – unutbu

+0

沒有probs大師shifu ...謝謝你的幫助! :) – NoobEditor