plot_football.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #!/usr/bin/env python
  2. """
  3. ========
  4. Football
  5. ========
  6. Load football network in GML format and compute some network statistcs.
  7. Shows how to download GML graph in a zipped file, unpack it, and load
  8. into a NetworkX graph.
  9. Requires Internet connection to download the URL
  10. http://www-personal.umich.edu/~mejn/netdata/football.zip
  11. """
  12. # Author: Aric Hagberg (hagberg@lanl.gov)
  13. # Copyright (C) 2007-2019 by
  14. # Aric Hagberg <hagberg@lanl.gov>
  15. # Dan Schult <dschult@colgate.edu>
  16. # Pieter Swart <swart@lanl.gov>
  17. # All rights reserved.
  18. # BSD license.
  19. try: # Python 3.x
  20. import urllib.request as urllib
  21. except ImportError: # Python 2.x
  22. import urllib
  23. import io
  24. import zipfile
  25. import matplotlib.pyplot as plt
  26. import networkx as nx
  27. url = "http://www-personal.umich.edu/~mejn/netdata/football.zip"
  28. sock = urllib.urlopen(url) # open URL
  29. s = io.BytesIO(sock.read()) # read into BytesIO "file"
  30. sock.close()
  31. zf = zipfile.ZipFile(s) # zipfile object
  32. txt = zf.read('football.txt').decode() # read info file
  33. gml = zf.read('football.gml').decode() # read gml data
  34. # throw away bogus first line with # from mejn files
  35. gml = gml.split('\n')[1:]
  36. G = nx.parse_gml(gml) # parse gml data
  37. print(txt)
  38. # print degree for each team - number of games
  39. for n, pen_weight in G.degree():
  40. print('%s %d' % (n, pen_weight))
  41. options = {
  42. 'node_color': 'black',
  43. 'node_size': 50,
  44. 'line_color': 'grey',
  45. 'linewidths': 0,
  46. 'width': 0.1,
  47. }
  48. nx.draw(G, **options)
  49. plt.show()