plot_degree_sequence.py 806 B

123456789101112131415161718192021222324252627282930313233343536
  1. """
  2. ===============
  3. Degree Sequence
  4. ===============
  5. Random graph from given degree sequence.
  6. """
  7. import matplotlib.pyplot as plt
  8. import networkx as nx
  9. # Specify seed for reproducibility
  10. seed = 668273
  11. z = [5, 3, 3, 3, 3, 2, 2, 2, 1, 1, 1]
  12. print(nx.is_graphical(z))
  13. print("Configuration model")
  14. G = nx.configuration_model(
  15. z, seed=seed
  16. ) # configuration model, seed for reproducibility
  17. degree_sequence = [d for n, d in G.degree()] # degree sequence
  18. print(f"Degree sequence {degree_sequence}")
  19. print("Degree histogram")
  20. hist = {}
  21. for d in degree_sequence:
  22. if d in hist:
  23. hist[d] += 1
  24. else:
  25. hist[d] = 1
  26. print("degree #nodes")
  27. for d in hist:
  28. print(f"{d:4} {hist[d]:6}")
  29. pos = nx.spring_layout(G, seed=seed) # Seed layout for reproducibility
  30. nx.draw(G, pos=pos)
  31. plt.show()