矩阵对象没有属性节点 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 数组。 returns 是根据 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
.
的内容保存为多种文件类型中的任何一种
我试图将一些数字表示为具有连通分量的图的边。为此,我一直在使用 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 数组。 returns 是根据 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
.