如何将元组的网络边缘属性列表提取到元组字典对(边缘标签)的字典中?
How to extract network edge attributes list of tuples to dictionary of tuple dictionary pairs(edge labels)?
我有一个带有边和边属性的网络图。我正在尝试使用
从边缘中提取边缘属性
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'}}
作为值的键和属性的元组字典。
用于 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},
(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'}}
我可以在 networkx
中想到两种解决此问题的好方法。第一种是为每个字段制作单独的标签,并用不同的颜色绘制它们,如下所示:
import networkx as nx
import matplotlib.pyplot as plt
# Create the graph from the example edgelist
edges=[(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'})]
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,pos,edge_labels=labels2,font_color='blue',label_pos=0.5,rotate=True)
nx.drawing.draw_networkx_edge_labels(G,pos,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,15))
pos=nx.spring_layout(G)
nx.draw(G,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,pos,edge_labels=custom_labels,font_color='red',
rotate=False,horizontalalignment ='left')
当然,您可以使用 figsize 和 font 参数来使这些更漂亮。此外,我个人建议使用 yED (https://www.yworks.com/products/yed) 或其他一些图形界面来处理此类事情。您可以使用 nx.write_graphml(G, "filename.graphml")
导出到 yED 可以读入的文件,然后使用它的 属性 映射器和布局工具进行设置。如果你要浏览大量的图,这会很乏味,但如果你想制作一个“最终版本”的图形,它确实是一个更好的工具,因为它很容易 fine-tune 放置单个节点,边缘和标签。 (这就是我如何为我的研究论文和会议幻灯片制作 99% 的网络图。)
EDIT 为了完整起见,我将把 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")
我有一个带有边和边属性的网络图。我正在尝试使用
从边缘中提取边缘属性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'}}
作为值的键和属性的元组字典。
用于 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},
(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'}}
我可以在 networkx
中想到两种解决此问题的好方法。第一种是为每个字段制作单独的标签,并用不同的颜色绘制它们,如下所示:
import networkx as nx
import matplotlib.pyplot as plt
# Create the graph from the example edgelist
edges=[(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'})]
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,pos,edge_labels=labels2,font_color='blue',label_pos=0.5,rotate=True)
nx.drawing.draw_networkx_edge_labels(G,pos,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,15))
pos=nx.spring_layout(G)
nx.draw(G,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,pos,edge_labels=custom_labels,font_color='red',
rotate=False,horizontalalignment ='left')
当然,您可以使用 figsize 和 font 参数来使这些更漂亮。此外,我个人建议使用 yED (https://www.yworks.com/products/yed) 或其他一些图形界面来处理此类事情。您可以使用 nx.write_graphml(G, "filename.graphml")
导出到 yED 可以读入的文件,然后使用它的 属性 映射器和布局工具进行设置。如果你要浏览大量的图,这会很乏味,但如果你想制作一个“最终版本”的图形,它确实是一个更好的工具,因为它很容易 fine-tune 放置单个节点,边缘和标签。 (这就是我如何为我的研究论文和会议幻灯片制作 99% 的网络图。)
EDIT 为了完整起见,我将把 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")