In [1]:
import networkx as nx

In [40]:
pd.read_csv('miles.dat')


---------------------------------------------------------------------------
CParserError                              Traceback (most recent call last)
<ipython-input-40-ce9617fef8f0> in <module>()
----> 1 pd.read_csv('miles.dat')

/Users/peacenick36/anaconda/lib/python2.7/site-packages/pandas/io/parsers.pyc in parser_f(filepath_or_buffer, sep, delimiter, header, names, index_col, usecols, squeeze, prefix, mangle_dupe_cols, dtype, engine, converters, true_values, false_values, skipinitialspace, skiprows, nrows, na_values, keep_default_na, na_filter, verbose, skip_blank_lines, parse_dates, infer_datetime_format, keep_date_col, date_parser, dayfirst, iterator, chunksize, compression, thousands, decimal, lineterminator, quotechar, quoting, escapechar, comment, encoding, dialect, tupleize_cols, error_bad_lines, warn_bad_lines, skipfooter, skip_footer, doublequote, delim_whitespace, as_recarray, compact_ints, use_unsigned, low_memory, buffer_lines, memory_map, float_precision)
    644                     skip_blank_lines=skip_blank_lines)
    645 
--> 646         return _read(filepath_or_buffer, kwds)
    647 
    648     parser_f.__name__ = name

/Users/peacenick36/anaconda/lib/python2.7/site-packages/pandas/io/parsers.pyc in _read(filepath_or_buffer, kwds)
    399         return parser
    400 
--> 401     data = parser.read()
    402     parser.close()
    403     return data

/Users/peacenick36/anaconda/lib/python2.7/site-packages/pandas/io/parsers.pyc in read(self, nrows)
    937                 raise ValueError('skipfooter not supported for iteration')
    938 
--> 939         ret = self._engine.read(nrows)
    940 
    941         if self.options.get('as_recarray'):

/Users/peacenick36/anaconda/lib/python2.7/site-packages/pandas/io/parsers.pyc in read(self, nrows)
   1506     def read(self, nrows=None):
   1507         try:
-> 1508             data = self._reader.read(nrows)
   1509         except StopIteration:
   1510             if self._first_chunk:

pandas/parser.pyx in pandas.parser.TextReader.read (pandas/parser.c:10415)()

pandas/parser.pyx in pandas.parser.TextReader._read_low_memory (pandas/parser.c:10691)()

pandas/parser.pyx in pandas.parser.TextReader._read_rows (pandas/parser.c:11437)()

pandas/parser.pyx in pandas.parser.TextReader._tokenize_rows (pandas/parser.c:11308)()

pandas/parser.pyx in pandas.parser.raise_parser_error (pandas/parser.c:27037)()

CParserError: Error tokenizing data. C error: Expected 2 fields in line 5, saw 3

In [36]:
from operator import itemgetter
import networkx as nx
import matplotlib.pyplot as plt
import random
import numpy as np

sizes = np.linspace(100,10000,10)
if __name__ == '__main__':
    # Create a BA model graph
    n=5
    m=4
    G=nx.generators.barabasi_albert_graph(n,m)
    # find node with largest degree
    node_and_degree=G.degree()
    #print node_and_degree
    (largest_hub,degree)=sorted(node_and_degree.items(),key=itemgetter(1))[-1]
    # Create ego graph of main hub
    hub_ego=nx.ego_graph(G,largest_hub)
    # Draw graph
    pos=nx.spring_layout(hub_ego)
    nx.draw(hub_ego,pos,node_color='b',node_size=sizes,with_labels=False)
    # Draw ego as large and red
    #nx.draw_networkx_nodes(hub_ego,pos,nodelist=[largest_hub],node_size=300,node_color='r')
    #plt.savefig('ego_graph.png')
    plt.show()



In [15]:
try:
    import matplotlib.pyplot as plt
except:
    raise

import networkx as nx

G=nx.Graph()

G.add_node('a',size=60)
G.add_node('b',size=40)

G.add_edge('a','b',weight=1)


elarge=[(u,v) for (u,v,d) in G.edges(data=True) if d['weight'] >0.5]
esmall=[(u,v) for (u,v,d) in G.edges(data=True) if d['weight'] <=0.5]

pos=nx.spring_layout(G) # positions for all nodes

# nodes
nx.draw_networkx_nodes(G,pos)#,node_size=700)

# edges
nx.draw_networkx_edges(G,pos,edgelist=elarge,
                    width=6)
nx.draw_networkx_edges(G,pos,edgelist=esmall,
                    width=6,alpha=0.5,edge_color='b',style='dashed')

# labels
nx.draw_networkx_labels(G,pos,font_size=20,font_family='sans-serif')

plt.axis('off')
plt.savefig("weighted_graph.png") # save as png
plt.show() # display



In [47]:
import networkx as nx
import pandas as pd
import numpy as np

def miles_graph():
    """ Return the cites example graph in miles_dat.txt
        from the Stanford GraphBase.
    """
    # open file miles_dat.txt.gz (or miles_dat.txt)
    import gzip
    fh = gzip.open('miles.dat.gz','r')

    G=nx.Graph()
    G.position={}
    G.population={}

    cities=[]
    for line in fh.readlines():
        line = line.decode()
        if line.startswith("*"): # skip comments
            continue

        numfind=re.compile("^\d+")

        if numfind.match(line): # this line is distances
            dist=line.split()
            for d in dist:
                G.add_edge(city,cities[i],weight=int(d))
                i=i+1
        else: # this line is a city, position, population
            i=1
            (city,coordpop)=line.split("[")
            cities.insert(0,city)
            (coord,pop)=coordpop.split("]")
            (y,x)=coord.split(",")

            G.add_node(city)
            # assign position - flip x axis for matplotlib, shift origin
            G.position[city]=(-int(x)+7500,int(y)-3000)
            G.population[city]=float(pop)/1000.0
    return G

if __name__ == '__main__':
    import networkx as nx
    import re
    import sys

    G=miles_graph()

    print("Loaded miles_dat.txt containing 128 cities.")
    print("digraph has %d nodes with %d edges"\
          %(nx.number_of_nodes(G),nx.number_of_edges(G)))


    # make new graph of cites, edge if less then 300 miles between them
    H=nx.Graph()
    for v in G:
        H.add_node(v)
    for (u,v,d) in G.edges(data=True):
        if d['weight'] < 300:
            H.add_edge(u,v)

    # draw with matplotlib/pylab

    try:
        import matplotlib.pyplot as plt
        plt.figure(figsize=(8,8))
        # with nodes colored by degree sized by population
        node_color=[float(H.degree(v)) for v in H]
        nx.draw(H,G.position,
             node_size=[G.population[v] for v in H],
             node_color=node_color,
             with_labels=False)

        # scale the axes equally
        plt.xlim(-5000,500)
        plt.ylim(-2000,3500)
        plt.show()
        #plt.savefig("knuth_miles.png")
    except:
        pass


Loaded miles_dat.txt containing 128 cities.
digraph has 128 nodes with 8128 edges

In [ ]: