如何解决如何将元组的网络边缘属性列表提取到元组字典对边缘标签的字典中?
从边缘提取边缘属性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}),1334448011,'Phone': 5392353776}),1244950426,{'Email': 'NJOHNSONJOHNSON34@GMAIL.COM'}),(1354581834,(1334448011,(1367797753,{'Email': 'NJOHNSONJOHNSON34@GMAIL.COM'})]
现在我想把它转换成
{(1334448011,1244950426): {'Email': 'NJOHNSONJOHNSON34@GMAIL.COM'},1367797753): {'Email': 'NJOHNSONJOHNSON34@GMAIL.COM','Phone': 5392353776},1334448011): {'Phone': 5392353776},1367797753): {'Phone': 5392353776},1334448011): {'Email': 'NJOHNSONJOHNSON34@GMAIL.COM',1354581834): {'Phone': 5392353776},'VIN': '1C3CDZBG9DN5907'}}
在 edge_labels 中使用
nx.draw_networkx_edge_labels(sub_gr,pos,edge_labels=edge_labels,font_color='red')
有没有办法做到这一点?
解决方法
假设模式总是相同的:edge_lables
的前两个元素应该是键,第三个元素是值,那么您可以使用字典理解。
d = {x[:2]: x[2:][0] for x in edge_labels}
{(1405394338,1367797753): {'Email': 'NJOHNSONJOHNSON34@GMAIL.COM','Phone': 5392353776,'VIN': '1C3CDZBG9DN5907'},(1405394338,1354581834): {'Phone': 5392353776},1334448011): {'Email': 'NJOHNSONJOHNSON34@GMAIL.COM','Phone': 5392353776},1244950426): {'Email': 'NJOHNSONJOHNSON34@GMAIL.COM'},(1354581834,1367797753): {'Phone': 5392353776},1334448011): {'Phone': 5392353776},(1334448011,(1367797753,1244950426): {'Email': 'NJOHNSONJOHNSON34@GMAIL.COM'}}
,
我可以在 networkx
中想到两种很好的方法来解决这个问题。第一种是为每个字段制作单独的标签,并用不同的颜色绘制它们,如下所示:
import networkx as nx
import matplotlib.pyplot as plt
# Create the graph from the example edgelist
edges=[(1405394338,1367797753,{'Email': 'NJOHNSONJOHNSON34@GMAIL.COM','VIN': '1C3CDZBG9DN5907'}),1354581834,{'Phone': 5392353776}),1334448011,'Phone': 5392353776}),1244950426,{'Email': 'NJOHNSONJOHNSON34@GMAIL.COM'}),{'Email': 'NJOHNSONJOHNSON34@GMAIL.COM'})]
G=nx.DiGraph(edges)
# Grab the labels individually
labels1=nx.get_edge_attributes(G,'Email')
labels2=nx.get_edge_attributes(G,'Phone')
labels3=nx.get_edge_attributes(G,'VIN')
# Setup the figure and plot it
plt.figure(figsize=(15,15))
pos=nx.spring_layout(G)
nx.draw(G,pos)
# Add each label individually
nx.drawing.draw_networkx_edge_labels(G,pos,edge_labels=labels1,font_color='red',label_pos=0.75,rotate=True)
nx.drawing.draw_networkx_edge_labels(G,edge_labels=labels2,font_color='blue',label_pos=0.5,edge_labels=labels3,font_color='green',label_pos=0.25,rotate=True)
# display
plt.show()
另一种是制作自定义标签,像这样:
# Setup the figure and plot it
plt.figure(figsize=(15,pos)
custom_labels = {}
for u,v,d in G.edges(data=True):
L=""
for att,val in d.items():
L+=att+":"+str(val)+"\n"
custom_labels[(u,v)]=L
nx.drawing.draw_networkx_edge_labels(G,edge_labels=custom_labels,rotate=False,horizontalalignment ='left')
当然,您可以使用 figsize 和 font 参数使这些更漂亮。此外,我个人建议使用 yED (https://www.yworks.com/products/yed) 或其他一些图形界面来处理此类事情。您可以使用 nx.write_graphml(G,"filename.graphml")
导出到 yED 可以读入的文件,然后使用它的属性映射器和布局工具进行设置。如果您要查看大量绘图,这会很乏味,但是如果您想制作“最终版本”图形,它确实是一个更好的工具,因为它可以轻松微调各个节点、边、和标签。 (这就是我为我的研究论文和会议幻灯片制作 99% 的网络数据的方式。)
编辑为了完整起见,我将把 yED 导出代码和我为它制作的数字放在这里:
# Make a copy for export
G_ex=G.copy()
# Add the custom labels we made earlier
# to the copy graph as an attribute
for u,v in custom_labels:
G_ex.edges[(u,v)]['label']=custom_labels[(u,v)]
# Convert the attributes to strings to avoid import headaches
for e in G_ex.edges():
for k,v in G_ex.edges[e].items():
G_ex.edges[e][k]=str(v)
# Actually do the exporting
nx.write_graphml(G_ex,"test.graphml")
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。