【发布时间】:2022-10-17 02:04:43
【问题描述】:
我使用 python 字典和 Networkx 创建了 2 个小图。
现在我想为节点和边分配并显示一个属性(容量)。当使用字典创建图表时,我没有这样做。我的代码是:
#VNFGraph
Nf = {'VNF1', 'VNF2', 'VNF3', 'VNF4', 'VNF5'}
Lf = {('VNF1', 'VNF2'),('VNF2', 'VNF3'),('VNF3', 'VNF4'), ('VNF2', 'VNF5'),('VNF5', 'VNF3')}
# SERVERGraph
Ns = {'S1', 'S2', 'S3', 'S4', 'S5'}
Ls = {('S1', 'S2'),('S1', 'S4'),('S2', 'S4'),('S2', 'S3'),('S4', 'S3'),('S4', 'S5'),('S3', 'S5')}
graph1 = nx.DiGraph()
graph1.add_nodes_from(list(Nf))
graph1.add_edges_from(list(Lf))
graph2 = nx.Graph()
graph2.add_nodes_from(list(Ns))
graph2.add_edges_from(list(Ls))
graph2.add_edge("S1", "S2", weight=4.7)
pos1 = ({'VNF1': (0, 6), 'VNF2': (2, 6), 'VNF3': (4, 6), 'VNF4': (6, 6), 'VNF5': (3, 4)})
pos = ({'S1': (0, 1), 'S2': (2, 2), 'S3': (4, 2), 'S4': (3, 1), 'S5': (6, 1)})
fig1, ax1 = plt.subplots(figsize=(7, 7))
nx.draw_networkx_nodes(graph1, pos=pos1, ax=ax1, edgecolors='black', node_size=1100)
nx.draw_networkx_labels(graph1, pos=pos1, ax=ax1, labels=dict(zip(Nf, Nf)), font_size=7)
nx.draw_networkx_edges(graph1, pos=pos1, ax=ax1, node_size=900, arrowsize=25)
nx.draw_networkx_nodes(graph2, pos=pos, ax=ax1, edgecolors='black', node_size=800)
nx.draw_networkx_labels(graph2, pos=pos, ax=ax1, labels=dict(zip(Ns, Ns)), font_size=8)
nx.draw_networkx_edges(graph2, pos=pos, ax=ax1, node_size=900, arrowsize=25)
plt.axis('on')
plt.show()
我想为每个节点分配值(容量属性)并链接并显示它。
【问题讨论】:
标签: python-3.x dictionary networking graph networkx