如何更改 networkx 图中的节点位置?
How to change node positions in networkx graph?
我想根据 Dictionary
个值更改 networkx.DiGraph
中的节点位置。
我的尝试:
1) 首先,我生成一个字典,其中 Keys
作为 Node
名称,Value
作为 X
和 Y
坐标的 Tuples
这是我的结果:
{'Start': (2, 2), 'S0': (0, 0), 'S1': (1, 0), 'S2': (2, 0), 'S3': (3, 0), 'S4': (4, 0), 'S5': (5, 0), 'S6': (6, 0), 'S7': (7, 0), 'S8': (8, 0), 'S9': (9, 0), 'S10': (10, 0), 'S11': (11, 0), 'S12': (12, 0), 'S13': (13, 0), 'S14': (14, 0), 'S15': (15, 0), 'S16': (16, 0), 'S17': (17, 0), 'S18': (18, 0), 'S19': (19, 0), 'S20': (20, 0), 'S21': (21, 0), 'S22': (22, 0), 'S23': (23, 0), 'S24': (24, 1), 'S25': (25, 1), 'S26': (26, 2), 'S27': (27, 2), 'S28': (28, 2), 'S29': (29, 3), 'S30': (30, 3), 'S31': (31, 3), 'S32': (32, 3), 'S33': (33, 3), 'S34': (34, 3), 'S35': (35, 3), 'S36': (36, 3), 'S37': (37, 3), 'S38': (38, 3), 'S39': (39, 3), 'S40': (40, 3), 'S41': (41, 3), 'S43': (43, 3), 'S44': (44, 3), 'S45': (45, 3), 'S47': (47, 3), 'S48': (48, 3), 'S49': (49, 3), 'S50': (50, 3), 'S51': (51, 3), 'S42': (42, 3), 'S46': (46, 3), 'Finish': (2, 2)}
2) 然后我从列表中生成 Nodes
并使用 G.add_nodes_from(nodes)
将其添加到图表中(下面是 nodes
列表):
['Start', 'S0', 'S1', 'S2', 'S3', 'S4', 'S5', 'S6', 'S7', 'S8', 'S9', 'S10', 'S11', 'S12', 'S13', 'S14', 'S15', 'S16', 'S17', 'S18', 'S19', 'S20', 'S21', 'S22', 'S23', 'S24', 'S25', 'S26', 'S27', 'S28', 'S29', 'S30', 'S31', 'S32', 'S33', 'S34', 'S35', 'S36', 'S37', 'S38', 'S39', 'S40', 'S41', 'S43', 'S44', 'S45', 'S47', 'S48', 'S49', 'S50', 'S51', 'S42', 'S46', 'Finish']
3)最后但同样重要的是我生成了边缘(数据太大无法放在这里)。
4)然后我就按照这个answer:
G = nx.DiGraph()
G.add_nodes_from(nodes) #Add nodes
#G.add_edges_from(edges) #DONT' use this line of code for reproduction
for node, pos in positions.items(): #Add Node positions
G.nodes[node]['pos'] = pos
5) 当我绘制图表时,这就是我得到的:
nx.draw_shell(G, with_labels = True, node_size = 500)
有什么问题?
- 如您所见,位置不是我定义的位置。
- 奇怪的是,当我用这个命令打印出
nodes
的属性时:pos = nx.get_node_attributes(G,'pos')
。我可以在正确的位置看到我所有的位置。
- 如何让图表在所需位置绘制
Nodes
?
编辑:
- 将命令从
nx.draw_shell
更改为 nx.draw
似乎没有帮助。
您可以使用 pos
参数将位置字典(键是节点,值是二维坐标)传递给更通用的 draw_networkx
函数:
nx.draw_networkx(G,pos= positions,with_labels=True,node_color=node_color,node_size=500)
请注意,nx.draw_shell
将根据 shell 布局算法对您的网络进行布局。
我想根据 Dictionary
个值更改 networkx.DiGraph
中的节点位置。
我的尝试:
1) 首先,我生成一个字典,其中 Keys
作为 Node
名称,Value
作为 X
和 Y
坐标的 Tuples
这是我的结果:
{'Start': (2, 2), 'S0': (0, 0), 'S1': (1, 0), 'S2': (2, 0), 'S3': (3, 0), 'S4': (4, 0), 'S5': (5, 0), 'S6': (6, 0), 'S7': (7, 0), 'S8': (8, 0), 'S9': (9, 0), 'S10': (10, 0), 'S11': (11, 0), 'S12': (12, 0), 'S13': (13, 0), 'S14': (14, 0), 'S15': (15, 0), 'S16': (16, 0), 'S17': (17, 0), 'S18': (18, 0), 'S19': (19, 0), 'S20': (20, 0), 'S21': (21, 0), 'S22': (22, 0), 'S23': (23, 0), 'S24': (24, 1), 'S25': (25, 1), 'S26': (26, 2), 'S27': (27, 2), 'S28': (28, 2), 'S29': (29, 3), 'S30': (30, 3), 'S31': (31, 3), 'S32': (32, 3), 'S33': (33, 3), 'S34': (34, 3), 'S35': (35, 3), 'S36': (36, 3), 'S37': (37, 3), 'S38': (38, 3), 'S39': (39, 3), 'S40': (40, 3), 'S41': (41, 3), 'S43': (43, 3), 'S44': (44, 3), 'S45': (45, 3), 'S47': (47, 3), 'S48': (48, 3), 'S49': (49, 3), 'S50': (50, 3), 'S51': (51, 3), 'S42': (42, 3), 'S46': (46, 3), 'Finish': (2, 2)}
2) 然后我从列表中生成 Nodes
并使用 G.add_nodes_from(nodes)
将其添加到图表中(下面是 nodes
列表):
['Start', 'S0', 'S1', 'S2', 'S3', 'S4', 'S5', 'S6', 'S7', 'S8', 'S9', 'S10', 'S11', 'S12', 'S13', 'S14', 'S15', 'S16', 'S17', 'S18', 'S19', 'S20', 'S21', 'S22', 'S23', 'S24', 'S25', 'S26', 'S27', 'S28', 'S29', 'S30', 'S31', 'S32', 'S33', 'S34', 'S35', 'S36', 'S37', 'S38', 'S39', 'S40', 'S41', 'S43', 'S44', 'S45', 'S47', 'S48', 'S49', 'S50', 'S51', 'S42', 'S46', 'Finish']
3)最后但同样重要的是我生成了边缘(数据太大无法放在这里)。
4)然后我就按照这个answer:
G = nx.DiGraph()
G.add_nodes_from(nodes) #Add nodes
#G.add_edges_from(edges) #DONT' use this line of code for reproduction
for node, pos in positions.items(): #Add Node positions
G.nodes[node]['pos'] = pos
5) 当我绘制图表时,这就是我得到的:
nx.draw_shell(G, with_labels = True, node_size = 500)
有什么问题?
- 如您所见,位置不是我定义的位置。
- 奇怪的是,当我用这个命令打印出
nodes
的属性时:pos = nx.get_node_attributes(G,'pos')
。我可以在正确的位置看到我所有的位置。 - 如何让图表在所需位置绘制
Nodes
?
编辑:
- 将命令从
nx.draw_shell
更改为nx.draw
似乎没有帮助。
您可以使用 pos
参数将位置字典(键是节点,值是二维坐标)传递给更通用的 draw_networkx
函数:
nx.draw_networkx(G,pos= positions,with_labels=True,node_color=node_color,node_size=500)
请注意,nx.draw_shell
将根据 shell 布局算法对您的网络进行布局。