【问题标题】:labeling networkx node attributes outside of nodes在节点外标记 networkx 节点属性
【发布时间】:2017-09-15 17:33:38
【问题描述】:

我正在研究属于{'human', 'machine'} 两种类型的小示例节点集,我想在networkx 图中的每个节点之外以字典形式标记节点属性,例如节点 c、e、j 中显示的那些在下图中。 (我使用 MS Word 在图表上添加字典类型的属性。):

使用以下代码生成基础图:

import networkx as nx
G = nx.Graph()
G.add_nodes_from(['a', 'b', 'c', 'd', 'e', 'f', 'g'], type = 'machine')
G.add_nodes_from(['h', 'i', 'j'], type = 'human')
G.add_edges_from([('a', 'c'), ('a', 'b'), ('a', 'd'), ('a', 'f'), ('b', 'd'), ('b', 'e'), ('b', 'g'), ('c', 'f'), ('c', 'd'), ('d', 'f'), ('d', 'e'), ('d', 'g'), ('e', 'g'), ('f', 'g'), ('f', 'h'), ('g', 'h'), ('h', 'i'), ('i', 'j')])

def plot_graph(G, weight_name=None):
    import matplotlib.pyplot as plt

    plt.figure()
    pos = nx.spring_layout(G)
    edges = G.edges()
    weights = None

    if weight_name:
        weights = [int(G[u][v][weight_name]) for u,v in edges]
        labels = nx.get_edge_attributes(G,weight_name)
        nx.draw_networkx_edge_labels(G,pos,edge_labels=labels)
        nx.draw_networkx(G, pos, edges=edges, width=weights);
    else:
        nx.draw_networkx(G, pos, edges=edges);

plot_graph(G, weight_name=None)
plt.savefig('example.png')
plt.show()

但是问题来了,

nx.get_node_attributes()nx.draw_networkx_labels() 函数将不包括标签上的字典键(在本例中为“类型”)(这仅适用于 nx.get_edge_attributes() 和 nx.draw_networkx_edge_labels()),如果有将使用nx.get_node_attributes()nx.draw_networkx_labels(),原始节点名称将被属性值替换。

我想知道是否有其他方法可以在每个节点外部以字典格式标记属性,同时将节点名称保留在节点内?我应该如何修改我当前的代码?

【问题讨论】:

    标签: python dictionary label networkx


    【解决方案1】:

    nx.draw_networkx_labels() 不包括键的问题可以通过创建一个新的字典来解决,该字典将代表整个字典的字符串保存为值。

    node_attrs = nx.get_node_attributes(G, 'type')
    custom_node_attrs = {}
    for node, attr in node_attrs.items():
        custom_node_attrs[node] = "{'type': '" + attr + "'}"
    

    关于绘制节点名称和属性,可以使用nx.draw()正常绘制节点名称,然后nx.draw_networkx_labels()绘制属性——这里可以手动移动属性位置到节点上方或下方。在下面的块中,pos 保存节点位置,pos_attrs 保存放置在适当节点上方的属性位置。

    pos_nodes = nx.spring_layout(G)
    pos_attrs = {}
    for node, coords in pos_nodes.items():
        pos_attrs[node] = (coords[0], coords[1] + 0.08)
    

    完整示例:

    import networkx as nx
    import matplotlib.pyplot as plt
    
    G = nx.Graph()
    G.add_nodes_from(['a', 'b', 'c', 'd', 'e', 'f', 'g'], type = 'machine')
    G.add_nodes_from(['h', 'i', 'j'], type = 'human')
    G.add_edges_from([('a', 'c'), ('a', 'b'), ('a', 'd'), ('a', 'f'), ('b', 'd'), ('b', 'e'), ('b', 'g'), ('c', 'f'), ('c', 'd'), ('d', 'f'), ('d', 'e'), ('d', 'g'), ('e', 'g'), ('f', 'g'), ('f', 'h'), ('g', 'h'), ('h', 'i'), ('i', 'j')])
    
    plt.figure()
    pos_nodes = nx.spring_layout(G)
    nx.draw(G, pos_nodes, with_labels=True)
    
    pos_attrs = {}
    for node, coords in pos_nodes.items():
        pos_attrs[node] = (coords[0], coords[1] + 0.08)
    
    node_attrs = nx.get_node_attributes(G, 'type')
    custom_node_attrs = {}
    for node, attr in node_attrs.items():
        custom_node_attrs[node] = "{'type': '" + attr + "'}"
    
    nx.draw_networkx_labels(G, pos_attrs, labels=custom_node_attrs)
    plt.show()
    

    输出:

    最后一个提示:如果您有很多边并且您的节点属性难以阅读,您可以尝试将边的颜色设置为较浅的灰色阴影。

    【讨论】:

      猜你喜欢
      • 2011-04-28
      • 1970-01-01
      • 2016-04-23
      • 2012-03-27
      • 1970-01-01
      • 2017-03-29
      • 2012-08-10
      • 2017-07-05
      • 2018-05-08
      相关资源
      最近更新 更多