plot_sampson.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. """
  2. =======
  3. Sampson
  4. =======
  5. Sampson's monastery data.
  6. Shows how to read data from a zip file and plot multiple frames.
  7. The data file can be found at:
  8. - https://github.com/networkx/networkx/blob/main/examples/drawing/sampson_data.zip
  9. """
  10. import zipfile
  11. from io import BytesIO as StringIO
  12. import matplotlib.pyplot as plt
  13. import networkx as nx
  14. with zipfile.ZipFile("sampson_data.zip") as zf:
  15. e1 = StringIO(zf.read("samplike1.txt"))
  16. e2 = StringIO(zf.read("samplike2.txt"))
  17. e3 = StringIO(zf.read("samplike3.txt"))
  18. G1 = nx.read_edgelist(e1, delimiter="\t")
  19. G2 = nx.read_edgelist(e2, delimiter="\t")
  20. G3 = nx.read_edgelist(e3, delimiter="\t")
  21. pos = nx.spring_layout(G3, iterations=100, seed=173)
  22. plt.clf()
  23. plt.subplot(221)
  24. plt.title("samplike1")
  25. nx.draw(G1, pos, node_size=50, with_labels=False)
  26. plt.subplot(222)
  27. plt.title("samplike2")
  28. nx.draw(G2, pos, node_size=50, with_labels=False)
  29. plt.subplot(223)
  30. plt.title("samplike3")
  31. nx.draw(G3, pos, node_size=50, with_labels=False)
  32. plt.subplot(224)
  33. plt.title("samplike1,2,3")
  34. nx.draw(G3, pos, edgelist=list(G3.edges()), node_size=50, with_labels=False)
  35. nx.draw_networkx_edges(G1, pos, alpha=0.25)
  36. nx.draw_networkx_edges(G2, pos, alpha=0.25)
  37. plt.tight_layout()
  38. plt.show()