2015-12-15 59 views
0

我已經從集合,networkx(如nx)和matplotlib(如plt)導入計數器。我已經在我製作的圖表中獲得了每個節點的度數。但是我想繪製度數與度數頻率的關係。 我嘗試如下:繪製Networkx的整數輸出

deg=list(G.degree().values())# set the degree for each node 
for i in deg:# iterate this list 
    count=Counter(i)#count the frequency of the nodes 
    plt.plot(count, i)#plot frequency vs degree 

不過,我不斷收到錯誤:

TypeError: 'int' object is not iterable 

所以我想知道如果我輸入的是正確的,也是,如果我的邏輯是正確的開始,因爲我我不確定這些頻率甚至會連接到它們的衍生程度,並正確地反映它們。

+1

顯示完整的回溯。它會告訴你在哪一行發生錯誤。 –

回答

2

您可能想嘗試networkx.degree_histogram,它可以計算出您想要的數據。它爲圖什麼G是

degseq = list(d for n, d in G.degree()) 
dmax = max(degseq) + 1 
freq = [ 0 for d in range(dmax) ] 
for d in degseq: 
    freq[d] += 1 
+0

感謝vey很多,但我不能得到輸出顯示與plt.show()...有什麼我失蹤? – james

+0

是的,plot,freq數組,說import matplotlib.pyplot爲plt,plt.hist(freq),plt.show()。 – Aric

0

你超過intlist迭代i,這很好,但後來這條線

count=Counter(i)#count the frequency of the nodes 

沒有意義,你就指望i,這是一個整數。你可能會想這樣做(假設這是collections.Counter),是

count = Counter(deg) 

這將計算所有出現在deg