plot_football.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. """
  2. ========
  3. Football
  4. ========
  5. Load football network in GML format and compute some network statistcs.
  6. Shows how to download GML graph in a zipped file, unpack it, and load
  7. into a NetworkX graph.
  8. Requires Internet connection to download the URL
  9. http://www-personal.umich.edu/~mejn/netdata/football.zip
  10. """
  11. import urllib.request
  12. import io
  13. import zipfile
  14. import matplotlib.pyplot as plt
  15. import networkx as nx
  16. url = "http://www-personal.umich.edu/~mejn/netdata/football.zip"
  17. sock = urllib.request.urlopen(url) # open URL
  18. s = io.BytesIO(sock.read()) # read into BytesIO "file"
  19. sock.close()
  20. zf = zipfile.ZipFile(s) # zipfile object
  21. txt = zf.read("football.txt").decode() # read info file
  22. gml = zf.read("football.gml").decode() # read gml data
  23. # throw away bogus first line with # from mejn files
  24. gml = gml.split("\n")[1:]
  25. G = nx.parse_gml(gml) # parse gml data
  26. print(txt)
  27. # print degree for each team - number of games
  28. for n, d in G.degree():
  29. print(f"{n:20} {d:2}")
  30. options = {"node_color": "black", "node_size": 50, "linewidths": 0, "width": 0.1}
  31. pos = nx.spring_layout(G, seed=1969) # Seed for reproducible layout
  32. nx.draw(G, pos, **options)
  33. plt.show()