Python NetworkX:如何将节点坐标指定为属性?

Python NetworkX:如何将节点坐标指定为属性?,python,graph,typeerror,networkx,Python,Graph,Typeerror,Networkx,在这样一个简单的图中: import networkx as nx import matplotlib.pyplot as plt G = nx.Graph() G.add_edge('0','1') G.add_edge('1','2') G.add_edge('2','0') G.add_edge('0','3') G.add_edge('1','4') G.add_edge('5','0') pos={'0':(1,0),'1':(1,1),'2':(2,3),'3':(3,2),'4

在这样一个简单的图中:

import networkx as nx
import matplotlib.pyplot as plt

G = nx.Graph()
G.add_edge('0','1')
G.add_edge('1','2')
G.add_edge('2','0')
G.add_edge('0','3')
G.add_edge('1','4')
G.add_edge('5','0')

pos={'0':(1,0),'1':(1,1),'2':(2,3),'3':(3,2),'4':(0.76,1.80),'5':(0,2)} #node:(x,y)
nx.draw(G,pos=pos,with_labels=True)
plt.show()
for i,n in enumerate(G.nodes()):
    G.nodes()[i]['weight']=[G.nodes()[i],pos[n]] #List of attributes
如果我尝试为每个节点分配一个包含节点ID及其
(x,y)
坐标的属性列表,如下所示:

import networkx as nx
import matplotlib.pyplot as plt

G = nx.Graph()
G.add_edge('0','1')
G.add_edge('1','2')
G.add_edge('2','0')
G.add_edge('0','3')
G.add_edge('1','4')
G.add_edge('5','0')

pos={'0':(1,0),'1':(1,1),'2':(2,3),'3':(3,2),'4':(0.76,1.80),'5':(0,2)} #node:(x,y)
nx.draw(G,pos=pos,with_labels=True)
plt.show()
for i,n in enumerate(G.nodes()):
    G.nodes()[i]['weight']=[G.nodes()[i],pos[n]] #List of attributes
我得到以下错误:

Traceback (most recent call last):

  File "<ipython-input-47-0f9ca94eeefd>", line 2, in <module>
    G.nodes()[i]['weight']=[G.nodes()[i],pos[n]] 

TypeError: 'str' object does not support item assignment
回溯(最近一次呼叫最后一次):
文件“”,第2行,在
G.nodes()[i]['weight']=[G.nodes()[i],位置[n]]
TypeError:“str”对象不支持项分配

这里出了什么问题?

经过一番研究,我发现答案在
nx.set\u node\u attributes()

当然,可以将节点位置指定为属性:

pos={'0':(1,0),'1':(1,1),'2':(2,3),'3':(3,2),'4':(0.76,1.80),'5':(0,2)}    
nx.set_node_attributes(G, pos, 'coord')
test={'0':55,'1':43,'2':17,'3':86,'4':2} #node '5' is missing
nx.set_node_attributes(G, 'test', test)
导致

In[1]: G.nodes(data=True)
Out[1]:
[('1', {'coord': (1, 1)}), #each node has its own position
 ('0', {'coord': (1, 0)}),
 ('3', {'coord': (3, 2)}),
 ('2', {'coord': (2, 3)}),
 ('5', {'coord': (0, 2)}),
 ('4', {'coord': (0.76, 1.8)})]
In[2]: G.nodes(data=True)
Out[2]:
[('1', {'coord': (1, 1), 'test': 43}),
 ('0', {'coord': (1, 0), 'test': 55}),
 ('3', {'coord': (3, 2), 'test': 86}),
 ('2', {'coord': (2, 3), 'test': 17}),
 ('5', {'coord': (0, 2)}),
 ('4', {'coord': (0.76, 1.8), 'test': 2})]
此外,还可以使用专用字典(在本例中为
test
)附加多个属性,这些字典不必具有与
G
中的节点相同数量的元素(例如,可能存在没有属性的节点):

导致

In[1]: G.nodes(data=True)
Out[1]:
[('1', {'coord': (1, 1)}), #each node has its own position
 ('0', {'coord': (1, 0)}),
 ('3', {'coord': (3, 2)}),
 ('2', {'coord': (2, 3)}),
 ('5', {'coord': (0, 2)}),
 ('4', {'coord': (0.76, 1.8)})]
In[2]: G.nodes(data=True)
Out[2]:
[('1', {'coord': (1, 1), 'test': 43}),
 ('0', {'coord': (1, 0), 'test': 55}),
 ('3', {'coord': (3, 2), 'test': 86}),
 ('2', {'coord': (2, 3), 'test': 17}),
 ('5', {'coord': (0, 2)}),
 ('4', {'coord': (0.76, 1.8), 'test': 2})]

我推测,使用。

如何知道由它创建的节点坐标并保存以供以后在另一个图形中使用?@Sigur本例中的节点坐标是虚构的。这取决于您试图解决的问题,但您可以为节点指定所需的坐标-由您决定!这些数据可以存储在字典中,然后可以另存为。这允许您导入和解包pickle,以便以后可以将其用于另一个图形。我希望这有帮助。如果你有一个更具体的问题,请提出一个新的问题和所有的细节。让我搜索一下。或多或少,我想要的是一系列具有相同顶点集但边根据某些参数显示的图。但是顶点集的位置首先是由一些布局创建的,然后对于其他图,我想使用相同的布局。但每次我运行代码时,图形都会发生变化@Sigur“每次我运行代码时,图形都会发生变化”->看起来好像有随机种子参与其中。种子(如果未指定)在每次迭代时都会更改。您可以在脚本的开头定义一个特定的种子,这样就可以复制相同的图形。您使用的是哪种图形生成函数?1/2我会的。谢谢