我試圖用連接的組件表示一些數字作爲圖的邊。爲此,我一直在使用python的networkX模塊。
我的圖是G,並具有初始化節點和邊如下:矩陣對象沒有屬性節點networkX python
G = nx.Graph()
for (x,y) in my_set:
G.add_edge(x,y)
print G.nodes() #This prints all the nodes
print G.edges() #Prints all the edges as tuples
adj_matrix = nx.to_numpy_matrix(G)
一旦我添加以下行,
pos = nx.spring_layout(adj_matrix)
我得到上述錯誤。 如果它可能有用,所有節點的編號都是9-15位。有412個節點和422個邊緣。
詳細的出錯:
File "pyjson.py", line 89, in <module>
mainevent()
File "pyjson.py", line 60, in mainevent
pos = nx.spring_layout(adj_matrix)
File "/usr/local/lib/python2.7/dist-packages/networkx/drawing/layout.py", line 244, in fruchterman_reingold_layout
A=nx.to_numpy_matrix(G,weight=weight)
File "/usr/local/lib/python2.7/dist-packages/networkx/convert_matrix.py", line 128, in to_numpy_matrix
nodelist = G.nodes()
AttributeError: 'matrix' object has no attribute 'nodes'
編輯:下面解決。有用的信息:pos爲每個節點創建一個帶有座標的字典。做nx.draw(G,pos)創建一個pylab圖。但它不顯示它,因爲pylab不會自動顯示。
不清楚你在這裏嘗試的是什麼['spring_layout'](https://networkx.github.io/documentation/latest/reference/generated/networkx.drawing.layout.spring_layout.html)將圖形作爲第一個param,不是矩陣數組 – EdChum
@EdChum:我不知道,謝謝。我如何才能讓圖形顯示出來? nx.draw(G)不會拋出任何錯誤,但它也不會出現。 – tvishwa107
'sprint_layout'返回節點位置,您需要將此傳遞給'nx.draw',所以'nx.draw(G,nx.spring_layout(G))'會起作用,我可以發佈一個示例 – EdChum