Note
Click here to download the full example code
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
Traceback (most recent call last):
File "/usr/lib/python3.8/site-packages/sphinx_gallery/gen_rst.py", line 440, in _memory_usage
out = func()
File "/usr/lib/python3.8/site-packages/sphinx_gallery/gen_rst.py", line 425, in __call__
exec(self.code, self.globals)
File "/builddir/build/BUILD/networkx-networkx-2.4/examples/graph/plot_football.py", line 36, in <module>
sock = urllib.urlopen(url) # open URL
File "/usr/lib64/python3.8/urllib/request.py", line 222, in urlopen
return opener.open(url, data, timeout)
File "/usr/lib64/python3.8/urllib/request.py", line 525, in open
response = self._open(req, data)
File "/usr/lib64/python3.8/urllib/request.py", line 542, in _open
result = self._call_chain(self.handle_open, protocol, protocol +
File "/usr/lib64/python3.8/urllib/request.py", line 502, in _call_chain
result = func(*args)
File "/usr/lib64/python3.8/urllib/request.py", line 1348, in http_open
return self.do_open(http.client.HTTPConnection, req)
File "/usr/lib64/python3.8/urllib/request.py", line 1322, in do_open
raise URLError(err)
urllib.error.URLError: <urlopen error [Errno -2] Name or service not known>
# 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, d in G.degree():
print('%s %d' % (n, d))
options = {
'node_color': 'black',
'node_size': 50,
'line_color': 'grey',
'linewidths': 0,
'width': 0.1,
}
nx.draw(G, **options)
plt.show()
Total running time of the script: ( 0 minutes 0.005 seconds)