In [1]:
%matplotlib inline
import matplotlib.pyplot as plt
import networkx as nx
import numpy as np

In [51]:
xmax = 11
ymax =7
G = nx.grid_2d_graph(xmax,ymax)
print G.nodes()
pos = dict((n, n) for n in G.nodes())
labels = dict(((i, j), i+j*xmax) for i, j in G.nodes())
costs = dict(((i, j), np.random.randint(1,10)) for i, j in G.nodes())

plt.rcParams['figure.figsize'] = (xmax, ymax) 
nx.set_node_attributes(G, 'pos', pos)
nx.set_node_attributes(G, 'labels', labels)
nx.draw_networkx(G, pos=nx.get_node_attributes(G, 'pos'),
                 labels=nx.get_node_attributes(G, 'labels'),
                 with_labels=True, node_size=500)
epos = nx.spring_layout(G)
edge_labels = nx.get_edge_attributes(G,'state')
nx.draw_networkx_edge_labels(G, epos, labels = edge_labels)


[(7, 3), (10, 4), (1, 3), (9, 1), (6, 6), (3, 0), (8, 0), (5, 4), (2, 1), (5, 6), (2, 6), (1, 6), (9, 4), (5, 1), (2, 5), (8, 5), (0, 3), (7, 2), (4, 0), (1, 2), (9, 0), (3, 3), (2, 0), (8, 1), (10, 3), (7, 6), (4, 4), (10, 0), (6, 3), (1, 5), (3, 6), (2, 2), (8, 6), (5, 3), (4, 1), (1, 1), (6, 4), (3, 2), (0, 0), (5, 0), (7, 1), (4, 5), (0, 4), (5, 5), (1, 4), (6, 0), (7, 5), (2, 3), (10, 1), (9, 5), (9, 3), (4, 2), (1, 0), (9, 6), (6, 5), (3, 5), (0, 1), (8, 3), (7, 0), (4, 6), (10, 5), (9, 2), (5, 2), (6, 1), (3, 1), (10, 6), (8, 2), (0, 2), (7, 4), (0, 6), (6, 2), (4, 3), (0, 5), (10, 2), (3, 4), (2, 4), (8, 4)]
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-51-513db97717a9> in <module>()
     10 nx.set_node_attributes(G, 'pos', pos)
     11 nx.set_node_attributes(G, 'labels', labels)
---> 12 nx.set_edge_attributes(G,'costs',costs)
     13 nx.draw_networkx(G, pos=nx.get_node_attributes(G, 'pos'),
     14                  labels=nx.get_node_attributes(G, 'labels'),

C:\Anaconda\lib\site-packages\networkx\classes\function.pyc in set_edge_attributes(G, name, values)
    400     else:
    401         for (u, v), value in values.items():
--> 402             G[u][v][name] = value
    403 
    404 

C:\Anaconda\lib\site-packages\networkx\classes\graph.pyc in __getitem__(self, n)
    405         {1: {}}
    406         """
--> 407         return self.adj[n]
    408 
    409     def add_node(self, n, attr_dict=None, **attr):

KeyError: 7

In [49]:
for y in xrange(1,ymax-1):
    for x in xrange(1,xmax-1):
        #print "(%s, %s) %s"%(x,y,G.node[(x,y)]['labels'])
        if np.random.randint(0,100)<=10:
            try:
                if len( G.neighbors((x,y)) ) >3 :
                    #print "(%s, %s) remove"%(x,y)
                    G.remove_nodes_from([(x,y)])
                    
            except KeyError as e:
                print e
plt.rcParams['figure.figsize'] = (xmax, ymax)                
nx.draw_networkx(G, pos=nx.get_node_attributes(G, 'pos'),
                 labels=nx.get_node_attributes(G, 'labels'),
                 with_labels=True, node_size=500)



In [ ]: