Networkx特定节点的标签 [英] Networkx specific nodes labeling
问题描述
我想绘制一个网络,但除了Cretin节点外,我希望它不被标记.
I want to draw a network and I want it to be unlabeled with the exception for cretin nodes.
我现在所拥有的是这样的:
What I have at the moment is something like this:
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')
此刻的代码更改了集线器列表中节点的大小和颜色.我也想给它们加上标签.
The code at the moment changes the size and color of the nodes in the hubs list. I'd like to label them as well.
我试图添加label参数并将其值设置为中心名称.但这没用.
I tried to add the label argument and set its value to the hub name. but it did't work.
谢谢
推荐答案
从布拉的评论来看,解决方案非常简单
From Bula's comment the solution is quite easy
诀窍是在字典中设置标签,其中的键是节点名,值是您需要的标签.因此,仅标记集线器,代码将类似于以下内容:
The trick is to set the labels in a dictionary where the key is the node name and the value is the label you require. Therefore to label only the hubs, the code will be something similar to this:
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,pos,labels,font_size=16,font_color='r')
我得到的是以下内容:
I got what I wanted which is the following:
我希望这会帮助像我这样的其他python/networkx新手:)
I hope this would help other python/networkx newbies like myself :)
再次感谢布拉(Bula)
Again, Thanks Bula
这篇关于Networkx特定节点的标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!