简体   繁体   English

如何仅在networkx图上显示重要节点的名称?

[英]How to only show important node's name on the networkx graph?

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. 我现在只能显示所有名称。

Graph:the result of the following codes 图:以下代码的结果

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 df.iloc [:,0]]中的v,with_labels = True)

nx.draw has an argument labels , which in combination with with_labels=True can draw only labels you want, only how you want. nx.draw有一个nx.draw label ,它与with_labels=True可以仅绘制所需的标签,仅绘制所需的标签。

labels (dictionary, optional (default=None)) – Node labels in a dictionary keyed by node of text labels 标签(字典,可选(默认=无))–字典中的节点标签,由文本标签的节点作为键

For example, you can pick nodes 'label' parameter and draw labels for nodes that have 3 or more neighbours: 例如,您可以选择节点的'label'参数并为具有3个或更多邻居的节点绘制标签:

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)

PS I don't recommend to use basic networkx drawing functional. PS我不建议使用基本的networkx绘图功能。 There are many powerful visualization libraries better than networkx. 有许多强大的可视化库比networkx更好。 Even in networkx docs you can find the same opinion. 即使在networkx文档中,您也可以找到相同的观点。 One can use Gephi , Graphviz (with various libraries) or Cytoscape for really HUGE graphs. 可以将GephiGraphviz (具有各种库)或Cytoscape用于真正的巨大图形。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM