2015-04-03 18 views
3

我有許多想要形象化的組件的圖。作爲一個特殊的特徵,巨組件中節點的節點應按照它們的特徵向量中心性進行縮放。所有其他節點具有相同的大小。使用多個組件繪製圖形時節點大小不正確

我使用下面的腳本:

import networkx as nx 
import pylab as py 
import matplotlib.pyplot as plt 

H = nx.read_gexf(input_file) 
print nx.info(H) 
#Name: 
#Type: Graph 
#Number of nodes: 719 
#Number of edges: 620 
#Average degree: 1.7246 

# Set draw() parameters 
node_sizes = dict.fromkeys(H.nodes(), 0.005) 
# Make node size of giant component nodes proportional to their eigenvector 
eigenvector = nx.eigenvector_centrality_numpy(G) 
for node, value in eigenvector.iteritems(): 
    node_sizes[node] = round(value, 4) 
node_sizes = [v*2000 for v in node_sizes.values()] # rescale 
node_positions = nx.pygraphviz_layout(H, prog="neato") 

# Draw graph with different color for each connected subgraph 
plt.figure(3, figsize=(90,90)) 
nx.draw(H, font_size=10, pos=node_positions, node_size=node_sizes, vmin=0.0, vmax=1.0, with_labels=True) 
plt.show() 

一切都是非常正確,我在不同輸出檢查。但是,我收到一個輸出,其中來自巨型組件以外的組件的一些節點是可擴展的。此外,巨型組件中的節點未正確縮放。

該快照顯示了巨大的分量和關閉組件與縮放節點: enter image description here

但是,如果我只使用字典eigenvector的節點尺寸打印的巨組G,我得到如下 - 正確 - 輸出(:

enter image description here

我做了一些故障排除,太例如,詞典/名單node_sizes是正確有趣的是,採用了隨機圖。返回正確的結果。所以我完全不知道我的H有什麼問題。

+0

- 但因爲我沒有你輸入網絡沒有測試它。請檢查它並讓我知道 - 特別是如果它不起作用。 – Joel 2015-04-03 20:32:38

回答

2

您會注意到node_sizes是一個列表。您尚未將繪製命令發送給節點列表。它將從網絡中的節點實時生成它們。當這兩個列表最終以不同的順序出現時,就會出現問題。我認爲這不是一個有多個組件的問題,而是您的網絡越大,它們就不會被放入相同的順序。

因此,而不是

node_sizes = [v*2000 for v in node_sizes.values()] 

使用

nodelist, node_sizes = zip(*node_sizes.items()) 

此節點列表將得到node_sizes.items和node_sizes的每個條目的第一個號碼的名單將獲得第二個數字的列表每個條目。

然後在繪圖命令給它我提供了一個答案的節點列表

nx.draw(H, font_size=10, pos=node_positions, node_size=node_sizes, vmin=0.0, vmax=1.0, with_labels=True, nodelist=nodelist) 
+0

它的工作原理。我期望這樣的事情 - 在哪裏可以發生錯誤。爲了縮放節點尺寸(我使用了2000倍),現在必須在特徵向量循環中以及在'node_sizes'字典的初始化中進行乘法運算。 – MERose 2015-04-03 20:48:27