python – 网络特定节点标签
发布时间:2020-09-16 07:05:22 所属栏目:Python 来源:互联网
导读:我想画一个网络,我想要它是没有标签的cretin节点的例外. 目前我所拥有的是这样的: nx.draw(G, pos=pos, node_color=b, node_size=8, with_labels=False)for hub in hubs: nx.draw_networkx_nodes(G, pos, nodelist=[hub[0]], node_color=r)
我想画一个网络,我想要它是没有标签的cretin节点的例外. 目前我所拥有的是这样的: nx.draw(G,pos=pos,node_color='b',node_size=8,with_labels=False) for hub in hubs: nx.draw_networkx_nodes(G,pos,nodelist=[hub[0]],node_color='r') 此时的代码会更改集线器列表中节点的大小和颜色.我也想标注他们. 我尝试添加标签参数并将其值设置为集线器名称.但它没有奏效. 谢谢 解决方法从布拉的评论,解决方案很容易诀窍是将标签设置在字典中,其中键是节点名称,值是您需要的标签.因此,为了仅标记集线器,代码将与此类似: labels = {} for node in G.nodes(): if node in hubs: #set the node name as the key and the label as its value labels[node] = node #set the argument 'with labels' to False so you have unlabeled graph nx.draw(G,with_labels=False) #Now only add labels to the nodes you require (the hubs in my case) nx.draw_networkx_labels(G,labels,font_size=16,font_color='r') 我得到了我想要的是以下内容: 我希望这将有助于其他python / networkx新手像我自己:) 再次感谢布拉 (编辑:甘南站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |