Batoul Diab
Batoul Diab

Reputation: 13

networkx graph: drawing the graph for a network after deleting nodes

Can anyone help with this. I am getting a network and drawing it as a graph as bellow:

g = nx.DiGraph()
//creating the graph......
print(nx.info(g))

nx.info(g) is giving me that number of nodes is 18772.

I got the graph plot for it as follows:

First Graph

however after deleting 30% of the most important nodes as bellow:

thirty_percent = int(g.number_of_nodes() * 0.30)
temp_g = g.copy()
print('initial number of nodes:', g.number_of_nodes())
print('nodes deleted (30%):', thirty_percent)
mostImportantNodes = sorted(nx.degree_centrality(g).items(), key=lambda x: x[1], reverse=True)
for n in range(0, thirty_percent):
    node = mostImportantNodes[n][0]
    temp_g.remove_node(node)
print(nx.info(temp_g))
# drawing the network graph
print('Graph after removing 30% of the most important nodes:')
print(datetime.now())
plt.clf()
pos = nx.spring_layout(temp_g)
nx.draw_networkx_nodes(temp_g, pos, node_size=10)
nx.draw_networkx_edges(temp_g, pos, edge_color='black', alpha=0.1, arrows=True)
plt.show()
print(datetime.now())

It is giving the right info about the new graph, but the graph plot is very bad, it seems like the nodes were added not deleted!

initial number of nodes: 18772
nodes deleted (30%): 5631
Name: 
Type: DiGraph
Number of nodes: 13141
Number of edges: 44911
Average in degree:   3.4176
Average out degree:   3.4176
highest 5 degrees:  [40, 40, 38, 36, 36]

Second Graph

Upvotes: 0

Views: 376

Answers (0)

Related Questions