gpt4 book ai didi

python - 在 NetworkX 中显示图形时,边对顺序 (u,v) vs (v,u) 是否重要?

转载 作者:太空宇宙 更新时间:2023-11-04 05:13:20 30 4
gpt4 key购买 nike

我正在从 Matlab 获取环形网络的边缘列表。然后 NetworkX 读取这个列表并以圆形布局绘制图形。我的边缘列表如下所示:

['1,2', '1,3', '1,49', '1,50', '2,3', '2,4', '2,50', '3,4', '3,5', '4,5', '4,6', '5,6', '5,7', '6,7', '6,8', '7,8', '7,9', '8,9', '8,10', '9,10', '9,11', '10,11', '10,12', '11,12', '11,13', '12,13', '12,14', '13,14', '13,15', '14,15', '14,16', '15,16', '15,17', '16,17', '16,18', '17,18', '17,19', '18,19', '18,20', '19,20', '19,21', '20,21', '20,22', '21,22', '21,23', '22,23', '22,24', '23,24', '23,25', '24,25', '24,26', '25,26', '25,27', '26,27', '26,28', '27,28', '27,29', '28,29', '28,30', '29,30', '29,31', '30,31', '30,32', '31,32', '31,33', '32,33', '32,34', '33,34', '33,35', '34,35', '34,36', '35,36', '35,37', '36,37', '36,38', '37,38', '37,39', '38,39', '38,40', '39,40', '39,41', '40,41', '40,42', '41,42', '41,43', '42,43', '42,44', '43,44', '43,45', '44,45', '44,46', '45,46', '45,47', '46,47', '46,48', '47,48', '47,49', '48,49', '48,50', '49,50']

注意,有些对是 '1,50','2,50' 等等,而其余对是递增顺序,'1,2','1,3','4,5 ' 等等。虽然构建图形不是问题,但是当我以圆形布局绘制它时,我得到了这个:

Ring Layout with 50 nodes

在右侧,圆圈以第 48 个节点结束,为了视觉一致性,它应该以第 50 个节点结束。我怀疑是有序对“1,50”、“2,50”等的原因。除了手动更改有序对之外,这是绘图功能的基本功能还是我可以做些什么?用于绘图的说明是:

f = open('Adj1.txt', 'r')
read_data = f.read()
f.close()
edge_list = read_data.split(sep='\n')
edge_list = edge_list[1:len(edge_list)-1]
H = nx.parse_edgelist(edge_list,delimiter=',')
....
nx.draw_circular(H,with_labels=False, node_size=25, node_color=node_color)

最佳答案

因为您的图是一个简单的无向图,所以重要的不是 (u,v) 的顺序,而是边列表的顺序。显然 networkx 根据 edge_list 中提到的顺序布置顶点(这在某种程度上是有道理的)问题可以演示如下:如果网络节点 50 出现在列表的前面:

import networkx as nx
edge_list=['1,2', '1,3', '1,49', '1,50', '2,3', '2,4', '2,50', '3,4', '3,5', '4,5', '4,6', '5,6', '5,7', '6,7', '6,8', '7,8', '7,9', '8,9', '8,10', '9,10', '9,11', '10,11', '10,12', '11,12', '11,13', '12,13', '12,14', '13,14', '13,15', '14,15', '14,16', '15,16', '15,17', '16,17', '16,18', '17,18', '17,19', '18,19', '18,20', '19,20', '19,21', '20,21', '20,22', '21,22', '21,23', '22,23', '22,24', '23,24', '23,25', '24,25', '24,26', '25,26', '25,27', '26,27', '26,28', '27,28', '27,29', '28,29', '28,30', '29,30', '29,31', '30,31', '30,32', '31,32', '31,33', '32,33', '32,34', '33,34', '33,35', '34,35', '34,36', '35,36', '35,37', '36,37', '36,38', '37,38', '37,39', '38,39', '38,40', '39,40', '39,41', '40,41', '40,42', '41,42', '41,43', '42,43', '42,44', '43,44', '43,45', '44,45', '44,46', '45,46', '45,47', '46,47', '46,48', '47,48', '47,49', '48,49', '48,50', '49,50']
H = nx.parse_edgelist(edge_list,delimiter=',')
nx.draw_circular(H,with_labels=False, node_size=25)

Bad

networkx 挣扎。然而,当我们将 1,50 和 consorts 放在列表的末尾(它们所属的位置!)时,它绘制得很好。

edge_list=['1,2', '1,3',  '2,3', '2,4',  '3,4', '3,5', '4,5', '4,6', '5,6', '5,7', '6,7', '6,8', '7,8', '7,9', '8,9', '8,10', '9,10', '9,11', '10,11', '10,12', '11,12', '11,13', '12,13', '12,14', '13,14', '13,15', '14,15', '14,16', '15,16', '15,17', '16,17', '16,18', '17,18', '17,19', '18,19', '18,20', '19,20', '19,21', '20,21', '20,22', '21,22', '21,23', '22,23', '22,24', '23,24', '23,25', '24,25', '24,26', '25,26', '25,27', '26,27', '26,28', '27,28', '27,29', '28,29', '28,30', '29,30', '29,31', '30,31', '30,32', '31,32', '31,33', '32,33', '32,34', '33,34', '33,35', '34,35', '34,36', '35,36', '35,37', '36,37', '36,38', '37,38', '37,39', '38,39', '38,40', '39,40', '39,41', '40,41', '40,42', '41,42', '41,43', '42,43', '42,44', '43,44', '43,45', '44,45', '44,46', '45,46', '45,47', '46,47', '46,48', '47,48', '47,49', '48,49', '48,50', '49,50','1,49', '1,50','2,50',]
H = nx.parse_edgelist(edge_list,delimiter=',')
nx.draw_circular(H,with_labels=False, node_size=25)

Good

(u,v)(v,u) 的顺序 确实 如果您的图是定向<当然很重要/strong>.

关于python - 在 NetworkX 中显示图形时,边对顺序 (u,v) vs (v,u) 是否重要?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42428891/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com