Networkx特定节点标记

2024-04-28 15:47:43 发布

您现在位置: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')

此时的代码会更改集线器列表中节点的大小和颜色。我也想给它们贴上标签。

我试图添加label参数并将其值设置为集线器名称。但没用。

谢谢


Tags: pos网络nodesizelabels节点with情况
1条回答
网友
1楼 · 发布于 2024-04-28 15:47:43

从布拉的评论来看,解决这个问题很容易

诀窍是在字典中设置标签,其中键是节点名,值是所需的标签。因此,要仅标记集线器,代码将类似于以下内容:

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')

我得到了我想要的是: enter image description here

我希望这能帮助其他python/networkx新手,比如我:)

再次感谢布拉

相关问题 更多 >