Statistics
| Branch: | Revision:

iof-tools / networkxMiCe / networkx-master / examples / graph / plot_football.py @ 5cef0f13

History | View | Annotate | Download (1.44 KB)

1
#!/usr/bin/env python
2
"""
3
========
4
Football
5
========
6

7
Load football network in GML format and compute some network statistcs.
8

9
Shows how to download GML graph in a zipped file, unpack it, and load
10
into a NetworkX graph.
11

12
Requires Internet connection to download the URL
13
http://www-personal.umich.edu/~mejn/netdata/football.zip
14
"""
15
# Author: Aric Hagberg (hagberg@lanl.gov)
16

    
17
#    Copyright (C) 2007-2019 by
18
#    Aric Hagberg <hagberg@lanl.gov>
19
#    Dan Schult <dschult@colgate.edu>
20
#    Pieter Swart <swart@lanl.gov>
21
#    All rights reserved.
22
#    BSD license.
23

    
24
try:  # Python 3.x
25
    import urllib.request as urllib
26
except ImportError:  # Python 2.x
27
    import urllib
28
import io
29
import zipfile
30

    
31
import matplotlib.pyplot as plt
32
import networkx as nx
33

    
34
url = "http://www-personal.umich.edu/~mejn/netdata/football.zip"
35

    
36
sock = urllib.urlopen(url)  # open URL
37
s = io.BytesIO(sock.read())  # read into BytesIO "file"
38
sock.close()
39

    
40
zf = zipfile.ZipFile(s)  # zipfile object
41
txt = zf.read('football.txt').decode()  # read info file
42
gml = zf.read('football.gml').decode()  # read gml data
43
# throw away bogus first line with # from mejn files
44
gml = gml.split('\n')[1:]
45
G = nx.parse_gml(gml)  # parse gml data
46

    
47
print(txt)
48
# print degree for each team - number of games
49
for n, d in G.degree():
50
    print('%s %d' % (n, d))
51

    
52
options = {
53
    'node_color': 'black',
54
    'node_size': 50,
55
    'line_color': 'grey',
56
    'linewidths': 0,
57
    'width': 0.1,
58
}
59
nx.draw(G, **options)
60
plt.show()