1 load_data = sio.loadmat(load_path) 2 3 #閾值處理 4 mat=np.array(load_data['R']) 5 mat[mat<0]=0 6 mat[mat<0.45]=0 7 8 #寫入txt文件 9 # np.savetxt(thr_path, mat); 10 11 #計算degree 12 g = nx.Graph() 13 for i in range(len(mat)): 14 for j in range(len(mat)): 15 if(mat[i,j]>0): #刪除邊的權重小於等於0的邊 16 g.add_edge(i, j,weight=mat[i,j]) 17 18 # nx.draw(g) 19 # nx.draw(g,with_labels=True) # 將標號標出來 20 # plt.show() 21 22 # 按權重划分為重權值得邊和輕權值的邊 23 elarge = [(u, v) for (u, v, d) in g.edges(data=True) if d['weight'] > 0.5] 24 esmall = [(u, v) for (u, v, d) in g.edges(data=True) if d['weight'] <= 0.5] 25 # 節點位置 26 pos = nx.spring_layout(g) # positions for all nodes 27 # 首先畫出節點位置 28 # nodes 29 nx.draw_networkx_nodes(g, pos, node_color='b',node_size=300,alpha=0.4) 30 # 根據權重,實線為權值大的邊,虛線為權值小的邊 31 # edges 32 nx.draw_networkx_edges(g, pos, edgelist=elarge, 33 width=3) 34 nx.draw_networkx_edges(g, pos, edgelist=esmall, 35 width=1, alpha=0.5, edge_color='g', style='dashed') 36 37 # labels標簽定義 38 nx.draw_networkx_labels(g, pos, font_size=13, font_family='sans-serif') 39 40 plt.axis('off') 41 plt.savefig("weighted_graph.png") # save as png 42 plt.show()
Result:
局部放大: