簡體   English   中英

矩陣對象沒有屬性節點networkX python

[英]Matrix object has no attribute nodes networkX python

我試圖將一些數字表示為具有連接組件的圖形的邊緣。 為此,我一直在使用python的networkX模塊。
我的圖是G,並且具有如下初始化的節點和邊:

    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將網絡圖作為第一個參數,而不是一個numpy數組。 根據Fruchterman-Reingold力導向算法,它返回的是節點的位置。

因此,您需要將其傳遞給draw示例:

import networkx as nx
%matplotlib inline
G=nx.lollipop_graph(14, 3)
nx.draw(G,nx.spring_layout(G))

收益率:

在此處輸入圖片說明

(部分答案解決了您評論中的某些問題。您可以將其添加到問題中,以便以后的用戶獲得更多的上下文信息)

pos使用每個節點的坐標創建字典。 執行nx.draw(G,pos)創建一個pylab圖形。 但是它不會顯示,因為pylab不會自動顯示。

import networkx as nx
import pylab as py

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
pos = nx.spring_layout(G)
nx.draw(G,pos)
py.show() # or py.savefig('graph.pdf') if you want to create a pdf, 
          # similarly for png or other file types

最終的py.show()將顯示它。 py.savefig('filename.extension')將根據您使用的extension py.savefig('filename.extension')另存為多種文件類型中的任何一種。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM