代码之家  ›  专栏  ›  技术社区  ›  Chris T.

在节点外标记networkx节点属性

  •  6
  • Chris T.  · 技术社区  · 8 年前

    {'human', 'machine'} networkx 如下图中节点c、e、j所示。(我使用MS Word在图表上添加字典类型属性。)

    enter image description here

    底图使用以下代码生成:

    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\u node\u attributes() ,原始节点名称将替换为属性值。

    1 回复  |  直到 8 年前
        1
  •  19
  •   edo    8 年前

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

    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() 通常绘制节点名称,然后 要绘制属性,可以手动将属性位置移动到节点上方或下方。在下面的区块中 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()
    

    输出:

    enter image description here

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