如何仅在networkx图上显示重要节点的名称? [英] How to only show important node's name on the networkx graph?

查看:1883
本文介绍了如何仅在networkx图上显示重要节点的名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该图看起来很杂乱,几乎看不到任何东西. 我只希望它显示高度集中的节点的名称, 但我不知道怎么做我现在只能显示所有名称.

The graph looks messy and barely recognize anything. I only want it to show the name of nodes with high centrality, but I don't know how. I can only show all the names now.

图形:以下代码的结果

G_D = nx.Graph()G_D.add_edges_from(G5.edges(data = True))

G_D=nx.Graph() G_D.add_edges_from(G5.edges(data=True))

nx.draw(G_D,nx.spring_layout(G_D),node_size = [v * 10 for v in df.iloc [:,0]],with_labels = True)

nx.draw(G_D,nx.spring_layout(G_D),node_size=[v * 10 for v in df.iloc[:,0]],with_labels= True)

推荐答案

nx.draw具有参数

nx.draw has an argument labels, which in combination with with_labels=True can draw only labels you want, only how you want.

标签(字典,可选(默认=无))–节点中的节点标签 用文本标签节点作为关键字的字典

labels (dictionary, optional (default=None)) – Node labels in a dictionary keyed by node of text labels

例如,您可以选择节点'label'参数并为具有3个或更多邻居的节点绘制标签:

For example, you can pick nodes 'label' parameter and draw labels for nodes that have 3 or more neighbours:

labels = {
    n: (G.nodes[n]['label']
        if len(list(nx.all_neighbors(G, n))) > 2
        else '')
    for n in G.nodes
}
nx.draw(G, with_labels=True, labels=labels)


P.S.我不建议使用基本的networkx绘图功能.有许多强大的可视化库比networkx更好.即使在networkx文档中 ,您也可以找到相同的观点.可以使用 Gephi Cytoscape 用于真正庞大的图.


P.S. I don't recommend to use basic networkx drawing functional. There are many powerful visualization libraries better than networkx. Even in networkx docs you can find the same opinion. One can use Gephi, Graphviz (with various libraries) or Cytoscape for really HUGE graphs.

这篇关于如何仅在networkx图上显示重要节点的名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆