plot_ego_graph.py 910 B

1234567891011121314151617181920212223242526272829303132333435
  1. """
  2. =========
  3. Ego Graph
  4. =========
  5. Example using the NetworkX ego_graph() function to return the main egonet of
  6. the largest hub in a Barabási-Albert network.
  7. """
  8. from operator import itemgetter
  9. import matplotlib.pyplot as plt
  10. import networkx as nx
  11. # Create a BA model graph - use seed for reproducibility
  12. n = 1000
  13. m = 2
  14. seed = 20532
  15. G = nx.barabasi_albert_graph(n, m, seed=seed)
  16. # find node with largest degree
  17. node_and_degree = G.degree()
  18. (largest_hub, degree) = sorted(node_and_degree, key=itemgetter(1))[-1]
  19. # Create ego graph of main hub
  20. hub_ego = nx.ego_graph(G, largest_hub)
  21. # Draw graph
  22. pos = nx.spring_layout(hub_ego, seed=seed) # Seed layout for reproducibility
  23. nx.draw(hub_ego, pos, node_color="b", node_size=50, with_labels=False)
  24. # Draw ego as large and red
  25. options = {"node_size": 300, "node_color": "r"}
  26. nx.draw_networkx_nodes(hub_ego, pos, nodelist=[largest_hub], **options)
  27. plt.show()