我有一个带有边和边属性的网络图。我正在尝试使用
sub_gr.edges(data=True)
edge_labels = list(sub_gr.edges(data=True))
[(1405394338,
1367797753,
{'Email': 'NJOHNSONJOHNSON34@GMAIL.COM',
'Phone': 5392353776,
'VIN': '1C3CDZBG9DN5907'}),
(1405394338, 1354581834, {'Phone': 5392353776}),
(1405394338,
1334448011,
{'Email': 'NJOHNSONJOHNSON34@GMAIL.COM', 'Phone': 5392353776}),
(1405394338, 1244950426, {'Email': 'NJOHNSONJOHNSON34@GMAIL.COM'}),
(1354581834, 1367797753, {'Phone': 5392353776}),
(1354581834, 1334448011, {'Phone': 5392353776}),
(1334448011,
1367797753,
{'Email': 'NJOHNSONJOHNSON34@GMAIL.COM', 'Phone': 5392353776}),
(1334448011, 1244950426, {'Email': 'NJOHNSONJOHNSON34@GMAIL.COM'}),
(1367797753, 1244950426, {'Email': 'NJOHNSONJOHNSON34@GMAIL.COM'})]
返回包含节点和边属性的元组列表
现在我想把它转换成
{(1334448011, 1244950426): {'Email': 'NJOHNSONJOHNSON34@GMAIL.COM'},
(1334448011, 1367797753): {'Email': 'NJOHNSONJOHNSON34@GMAIL.COM',
'Phone': 5392353776},
(1354581834, 1334448011): {'Phone': 5392353776},
(1354581834, 1367797753): {'Phone': 5392353776},
(1367797753, 1244950426): {'Email': 'NJOHNSONJOHNSON34@GMAIL.COM'},
(1405394338, 1244950426): {'Email': 'NJOHNSONJOHNSON34@GMAIL.COM'},
(1405394338, 1334448011): {'Email': 'NJOHNSONJOHNSON34@GMAIL.COM',
'Phone': 5392353776},
(1405394338, 1354581834): {'Phone': 5392353776},
(1405394338, 1367797753): {'Email': 'NJOHNSONJOHNSON34@GMAIL.COM',
'Phone': 5392353776,
'VIN': '1C3CDZBG9DN5907'}}
元组字典,作为值的键和属性
在边缘标签中使用的步骤
nx.draw_networkx_edge_labels(sub_gr,pos,edge_labels=edge_labels,font_color='red')
有办法做到这一点吗
假设模式总是相同的:
edge_lables
中的前两个元素应该是键,第三个元素是值,然后可以使用字典理解在
networkx
中,我可以想出两种很好的方法来解决这个问题。第一种方法是为每个字段制作单独的标签,并以不同的颜色打印,如下所示:本例中的图形如下所示:
另一个是制作自定义标签,如下所示:
在本例中,该图如下所示:
当然,您可以使用figsize和font参数使这些更漂亮。此外,我个人建议使用yED(https://www.yworks.com/products/yed)或其他图形界面来实现这类功能。您可以使用
nx.write_graphml(G, "filename.graphml")
导出到可以读入的文件,然后使用它的属性映射器和布局工具进行设置。如果要查看大量绘图,这会很乏味,但如果要制作“最终版本”图形,它确实是一个更好的工具,因为可以轻松微调各个节点、边和标签的位置。(这就是我如何为我的研究论文和会议幻灯片制作99%的网络数据。)编辑为完整起见,我将在此处输入导出代码和我为其制作的图形:
我将graphml文件导入到yED中,并对其进行处理,直到得到以下结果:
相关问题 更多 >
编程相关推荐