comparison env/share/doc/networkx-2.5/examples/drawing/plot_ego_graph.py @ 0:4f3585e2f14b draft default tip

"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
author shellac
date Mon, 22 Mar 2021 18:12:50 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4f3585e2f14b
1 """
2 =========
3 Ego Graph
4 =========
5
6 Example using the NetworkX ego_graph() function to return the main egonet of
7 the largest hub in a Barabási-Albert network.
8 """
9
10 from operator import itemgetter
11
12 import matplotlib.pyplot as plt
13 import networkx as nx
14
15 # Create a BA model graph
16 n = 1000
17 m = 2
18 G = nx.generators.barabasi_albert_graph(n, m)
19
20 # find node with largest degree
21 node_and_degree = G.degree()
22 (largest_hub, degree) = sorted(node_and_degree, key=itemgetter(1))[-1]
23
24 # Create ego graph of main hub
25 hub_ego = nx.ego_graph(G, largest_hub)
26
27 # Draw graph
28 pos = nx.spring_layout(hub_ego)
29 nx.draw(hub_ego, pos, node_color="b", node_size=50, with_labels=False)
30
31 # Draw ego as large and red
32 options = {"node_size": 300, "node_color": "r"}
33 nx.draw_networkx_nodes(hub_ego, pos, nodelist=[largest_hub], **options)
34 plt.show()