How to only show important node's name on the

2019-08-08 10:15发布

问题:

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.