当前位置:  开发笔记 > 编程语言 > 正文

Networkx特定节点标记

如何解决《Networkx特定节点标记》经验,为你挑选了1个好方法。

我想画一个网络,我希望它没有标记,但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参数并将其值设置为集线器名称.但它没有用.

谢谢



1> amaatouq..:

从Bula的评论来看,解决方案非常简单

诀窍是在字典中设置标签,其中键是节点名称,值是您需要的标签.因此,为了仅标记集线器,代码将类似于此:

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

我得到了我想要的以下内容: 在此输入图像描述

我希望这会有助于像我这样的其他python/networkx新手:)

再次,谢谢布拉

推荐阅读
刘美娥94662
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有