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

大兔子大兔子 提交于 2019-12-11 10:38:00

问题


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

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


回答1:


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

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. 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.



来源:https://stackoverflow.com/questions/55320418/how-to-only-show-important-nodes-name-on-the-networkx-graph

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!