Python networks change color of nodes when using draw_network_nodes()
Question: The goal is to obtain something similar to

![enter image description here](http://i.stack.imgur.com/TaCUR.jpg)

To define the graph I use:

    
    
    import matplotlib.pyplot as plt
    import networkx as nx
    graph = {
        '1': ['2', '3', '4'],
        '2': ['5','11','12','13','14','15'],
        '3' : ['6','7','66','77'],
        '5': ['6', '8','66','77'],
        '4': ['7','66','77'],
        '7': ['9', '10']
        } 
    
    MG = nx.DiGraph()
    MG.add_edges_from([(start, stop, {'weigth' :  len(graph[start]) }) 
         for start in graph for stop in graph[start]])
    

and the code to plot it is:

    
    
    plt.figure(figsize=(8,8))
    pos=nx.graphviz_layout(MG,prog="twopi",root='1')
    for n in MG.nodes_iter():
        nx.draw_networkx_nodes(MG, pos,
            nodelist = [n],
            node_size = 2000 / float(len(MG[n[0]])+1),
            node_color = (len(MG[n[0]])+1),
            alpha = 1/float(len(MG[n[0]])+1),
            with_labels=True
                           )
    xmax=1.1*max(xx for xx,yy in pos.values())
    ymax=1.1*max(yy for xx,yy in pos.values())
    plt.xlim(0,xmax)
    plt.ylim(0,ymax)
    plt.show()
    

There are 2 things here that I would like to know:

  * how can I have the color node depending on the number of links each node has (i.e. `len(MG[0])`)? 

  * where are my labels?

Thanks


Answer: How about this? You can use matplotlib's colormaps to map values to colors for
the nodes.

    
    
    import matplotlib.pyplot as plt
    import networkx as nx
    graph = {
        '1': ['2', '3', '4'],
        '2': ['5','11','12','13','14','15'],
        '3' : ['6','7','66','77'],
        '5': ['6', '8','66','77'],
        '4': ['7','66','77'],
        '7': ['9', '10']
        }
    
    MG = nx.DiGraph(graph)
    
    
    plt.figure(figsize=(8,8))
    pos=nx.graphviz_layout(MG,prog="twopi",root='1')
    
    nodes = MG.nodes()
    degree = MG.degree()
    color = [degree[n] for n in nodes]
    size = [2000 / (degree[n]+1.0) for n in nodes]
    
    nx.draw(MG, pos, nodelist=nodes, node_color=color, node_size=size,
            with_labels=True, cmap=plt.cm.Blues, arrows=False)
    plt.show()
    

![enter image description here](http://i.stack.imgur.com/21CBx.png)

