【问题标题】:Adding attribute (capacity) to node and edge in a graph created using dictionary向使用字典创建的图中的节点和边添加属性(容量)
【发布时间】: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


    【解决方案1】:

    您需要将容量作为值列表传递给 nx.draw_commands(与图形的节点/边的顺序相同)。我通常的做法是将属性写入图形,然后将它们读出并将它们传递给绘图命令:

    import networkx as nx 
    import matplotlib.pyplot as plt
    from random import randint
    
    Nf = {'VNF1', 'VNF2', 'VNF3', 'VNF4', 'VNF5'}
    Lf = {('VNF1', 'VNF2'),('VNF2', 'VNF3'),('VNF3', 'VNF4'), ('VNF2', 'VNF5'),('VNF5', 'VNF3')}
    
    graph1 = nx.DiGraph()
    graph1.add_nodes_from(list(Nf))
    graph1.add_edges_from(list(Lf))
    
    pos1 = ({'VNF1': (0, 6), 'VNF2': (2, 6), 'VNF3': (4, 6), 'VNF4': (6, 6), 'VNF5': (3, 4)})
    
    '''
    Create a dict of node and edge weights:
    Dict has to have following form: {node_label: {attribute1: value1, attribute2: value2 ...}}
    '''
    
    node_weight = {n:{'weight':randint(100, 1000)} for n in graph1.nodes()}
    edge_weight = {e:{'weight':randint(1, 5)} for e in graph1.edges()}
    
    '''
    Write attributes to graph
    '''
    nx.set_node_attributes(graph1, node_weight)
    nx.set_edge_attributes(graph1, edge_weight)
    
    '''
    Get attributes from graph; returns dict: {node_label: value (of specified attribtue)}
    In order to pass them to nx.draw you need a list, so unpack the values of the dict
    This way you avoid messing up the order of the nodes
    '''
    node_size = list(nx.get_node_attributes(graph1, 'weight').values())
    edge_size = list(nx.get_edge_attributes(graph1, 'weight').values())
    
    
    fig1, ax1 = plt.subplots(figsize=(7, 7))
    
    nx.draw_networkx_nodes(graph1, pos=pos1, ax=ax1, edgecolors='black', node_size=node_size)
    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, width = edge_size, arrowsize=25)
    
    plt.axis('on')
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多