import networkx as nx import matplotlib.pyplot as plt # define G as the "path graph" w/8 nodes G=nx.path_graph(8) # 3 commands needed to draw/plot/save nx.draw(G) #plt.savefig("simple_path.png") # save as png plt.show() # display # note that networkx handles *really* simple file input !rm simple.txt !touch simple.txt !echo "0 1" >> simple.txt !echo "1 2" >> simple.txt !echo "2 0" >> simple.txt !cat simple.txt G = nx.read_edgelist('simple.txt', create_using= nx.DiGraph()) nx.draw(G) plt.show() # display print("radius: %d" % nx.radius(G)) print("diameter: %d" % nx.diameter(G)) print("eccentricity: %s" % nx.eccentricity(G)) print("center: %s" % nx.center(G)) print("periphery: %s" % nx.periphery(G)) print("density: %s" % nx.density(G)) # one of the most famous graphs is the 34-node "karate club" graph G=nx.karate_club_graph() print("Node Degree") for v in G: print('%s %s' % (v,G.degree(v))) # here's an example of coloring pos=nx.spring_layout(G) nx.draw(G,pos,node_color=range(34),node_size=800,cmap=plt.cm.Blues) plt.savefig("node_colormap.png") # save as png plt.show() # display print("radius: %d" % nx.radius(G)) print("diameter: %d" % nx.diameter(G)) print("eccentricity: %s" % nx.eccentricity(G)) print("center: %s" % nx.center(G)) print("periphery: %s" % nx.periphery(G)) print("density: %s" % nx.density(G)) # let's try this for a 20-node star graph G=nx.star_graph(20) pos=nx.spring_layout(G) colors=range(20) nx.draw(G,pos,node_color='#A0CBE2',edge_color=colors,width=4,edge_cmap=plt.cm.Blues,with_labels=False) plt.savefig("edge_colormap.png") # save as png plt.show() # display for v in G: print('%s %s' % (v,G.degree(v))) print("radius: %d" % nx.radius(G)) print("diameter: %d" % nx.diameter(G)) print("eccentricity: %s" % nx.eccentricity(G)) print("center: %s" % nx.center(G)) print("periphery: %s" % nx.periphery(G)) print("density: %s" % nx.density(G)) # here's an example of networkx syntax for file i/o: from networkx import * import sys G=grid_2d_graph(5,5) # 5x5 grid try: # Python 2.6+ write_adjlist(G,sys.stdout) # write adjacency list to screen except TypeError: # Python 3.x write_adjlist(G,sys.stdout.buffer) # write adjacency list to screen # write edgelist to grid.edgelist write_edgelist(G,path="grid.edgelist",delimiter=":") # read edgelist from grid.edgelist H=read_edgelist(path="grid.edgelist",delimiter=":") # ok now something cool: an overlay of graph and its degree rank plot G = nx.gnp_random_graph(100,0.02) degree_sequence=sorted(nx.degree(G).values(),reverse=True) # degree sequence #print "Degree sequence", degree_sequence dmax=max(degree_sequence) plt.loglog(degree_sequence,'b-',marker='o') plt.title("Degree rank plot") plt.ylabel("degree") plt.xlabel("rank") # draw graph in inset plt.axes([0.45,0.45,0.45,0.45]) Gcc=sorted(nx.connected_component_subgraphs(G), key = len, reverse=True)[0] pos=nx.spring_layout(Gcc) plt.axis('off') nx.draw_networkx_nodes(Gcc,pos,node_size=20) nx.draw_networkx_edges(Gcc,pos,alpha=0.4) plt.savefig("degree_rank_plot.png") plt.show() #!/usr/bin/env python """ 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 by # Aric Hagberg # Dan Schult # Pieter Swart # All rights reserved. # BSD license. try: import pyparsing except ImportError as e: raise ImportError(str(e)+". Check http://pyparsing.wikispaces.com/") from networkx import * url="http://www-personal.umich.edu/~mejn/netdata/football.zip" try: # Python 3.x import urllib.request as urllib except ImportError: # Python 2.x import urllib import io import zipfile 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=parse_gml(gml) # parse gml data print(txt) # print degree for each team - number of games for n,d in G.degree_iter(): print('%s %d' % (n, d))