123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- #!/usr/bin/env python
- """
- ========
- Football
- ========
- Load football network in GML format and compute some network statistcs.
- Shows how to download GML graph in a zipped file, unpack it, and load
- into a NetworkX graph.
- Requires Internet connection to download the URL
- http://www-personal.umich.edu/~mejn/netdata/football.zip
- """
- # Author: Aric Hagberg (hagberg@lanl.gov)
- # Copyright (C) 2007-2019 by
- # Aric Hagberg <hagberg@lanl.gov>
- # Dan Schult <dschult@colgate.edu>
- # Pieter Swart <swart@lanl.gov>
- # All rights reserved.
- # BSD license.
- try: # Python 3.x
- import urllib.request as urllib
- except ImportError: # Python 2.x
- import urllib
- import io
- import zipfile
- import matplotlib.pyplot as plt
- import networkx as nx
- url = "http://www-personal.umich.edu/~mejn/netdata/football.zip"
- sock = urllib.urlopen(url) # open URL
- s = io.BytesIO(sock.read()) # read into BytesIO "file"
- sock.close()
- zf = zipfile.ZipFile(s) # zipfile object
- txt = zf.read('football.txt').decode() # read info file
- gml = zf.read('football.gml').decode() # read gml data
- # throw away bogus first line with # from mejn files
- gml = gml.split('\n')[1:]
- G = nx.parse_gml(gml) # parse gml data
- print(txt)
- # print degree for each team - number of games
- for n, pen_weight in G.degree():
- print('%s %d' % (n, pen_weight))
- options = {
- 'node_color': 'black',
- 'node_size': 50,
- 'line_color': 'grey',
- 'linewidths': 0,
- 'width': 0.1,
- }
- nx.draw(G, **options)
- plt.show()
|