本文介绍了在节点外部标记networkx节点属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我正在研究一个属于两个类型 {'human','machine'} 的小型示例节点集,我想以字典形式在节点属性之外标记节点属性 networkx 图中的每个节点,例如下图中的节点c,e,j中所示。 (我使用MS Word在图形上添加了字典类型的属性。):I am working on a small example node set belonging to two types {'human', 'machine'} and I want to label node attributes in dictionary form outside of each node in networkx graph, such as those shown in nodes c, e, j in the graph below. (I used MS Word to add dictionary-type attributes on the graph.):基本图使用以下代码生成:The base plot is generated using the following code:import networkx as nxG = 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()函数不会在标签上包含字典键(在此示例中为 type)(仅适用于nx.get_edge_attributes()和nx.draw_networkx_edge_labels()),以及是否要使用 nx.get_node_attributes()和 nx.draw_networkx_labels(),原来的节点名称将被属性值替换。the nx.get_node_attributes() and nx.draw_networkx_labels() functions will not include dictionary keys (in this example, 'type') on the labels (this works only for nx.get_edge_attributes() and nx.draw_networkx_edge_labels()), and if one would to use nx.get_node_attributes() and nx.draw_networkx_labels(), original node names will be replaced by attribute values.我想知道是否还有其他方法可以在每个节点之外以字典格式标记属性同时将节点名称保留在节点内?我应该如何修改当前代码?I am wondering if there're alternative methods to label attribute in dictionary format outside each node while keeping node names inside the nodes? How should I modify my current code?推荐答案 nx.draw_networkx_labels()不包含键的方法可以通过创建一个新的dict来解决,该字典将容纳代表整个dict的字符串作为值。The issue of nx.draw_networkx_labels() not including keys can be solved by creating a new dict that will hold strings representing whole dicts as values.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 保存适当位置上方的属性位置。Regarding drawing node names and attributes, you can use nx.draw() to normally draw node names and then nx.draw_networkx_labels() to draw attributes -- here you can manually shift attribute positions to be above or below nodes. In the block below pos holds node positions and pos_attrs holds attribute positions placed above appropriate nodes.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 nximport matplotlib.pyplot as pltG = 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()输出:最终版本提示:如果边缘很多,并且节点属性难以阅读,则可以尝试将边缘的颜色设置为浅灰色。A final tip: if you have many edges and your node attributes are hard to read, you can try to set the color of edges to a lighter shade of gray. 这篇关于在节点外部标记networkx节点属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 06-16 11:49