Silvan
Silvan

Reputation: 65

how to draw graph based on centrality?

I have analyzed my graph and got a eigenvector centrality. (show below)

cit = nx.read_edgelist('Cit-HepTh.txt', create_using=nx.DiGraph(), nodetype=int)
(...compute centrality to a dict...)

And now I want to draw a graph based on this dict where different nodes have a different size based on their centrality.
I have seen an example that draws different colors(Maybe I think there is a similar way to draw different size

node_colours = []
for node in nodes:
    if node < 5:
        node_colours.append('blue')
    else:
        node_colours.append('green')
#draw graph, substitute the single colour with ur list of colours
nx.draw_spring(G, k=1, node_color = node_colours, 
               node_size = 200, font_size = 6, with_labels = True)

And I am trying to draw a large graph which hax 27000+ nodes, but it seems that it is quite slow to draw such a graph for networkX. So I want to just draw the top 100(highest eigenVector Centrality). Do you have any examples or suggestions?

Upvotes: 2

Views: 2185

Answers (1)

skrubber
skrubber

Reputation: 1095

Something on these lines (quick and dirty though with 27k+ nodes):

After getting the centrality of each node by:

 eigen_centrality = nx.eigenvector_centrality(G)
 all_nodes = [(node,eigen_centrality(node)) for node in eigen_centrality]

Find the top 100 nodes by centrality:

top_100_nodes = [n for (n,c) in all_nodes if c in np.argsort(c)[-100:]]

And then, create a subgraph of the top 100 nodes:

G1 = G.subgraph(top_100_nodes)  

And then draw the Graph:

top_100_centrality = nx.eigenvector_centrality(G1)
nx.draw_spring(G1, k =1, node_color = node_colours, \
               node_size = [top_100_centrality(n) for n in G1.nodes()], 
               font_size = 6, with_labels = True)

Upvotes: 3

Related Questions