在节点外部标记networkx节点属性

Chr*_* T. 3 python label dictionary networkx

我正在研究一个属于两种类型的小型示例节点集{'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()
Run Code Online (Sandbox Code Playgroud)

但这是问题所在

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(),原来的节点名称将由属性值代替。

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

edo*_*edo 7

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 + "'}"
Run Code Online (Sandbox Code Playgroud)

关于绘制节点名称和属性,您可以用来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)
Run Code Online (Sandbox Code Playgroud)

完整示例:

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()
Run Code Online (Sandbox Code Playgroud)

输出:

在此处输入图片说明

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