How to add `colorbar` to `networkx` using a `seaborn` color palette? (Python 3)
Question: I'm trying to add a `colorbar` to my `networkx` drawn `matplotlib ax` from the
range of `1` (being the lightest) and `3` (being the darkest) [check out the
line w/ `cmap` below]. I'm trying to combine a lot of `PyData`
functionalities.

**How can I add a color bar type feature on a networkx plot using a seaborn
color palette?**

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

    
    
    # Set up Graph
    DF_adj = pd.DataFrame(np.array(
         [[1, 0, 1, 1],
         [0, 1, 1, 0],
         [1, 1, 1, 1],
         [1, 0, 1, 1] ]), columns=['sepal length (cm)', 'sepal width (cm)', 'petal length (cm)', 'petal width (cm)'], index=['sepal length (cm)', 'sepal width (cm)', 'petal length (cm)', 'petal width (cm)'])
    
    G = nx.Graph(DF_adj.as_matrix())
    G = nx.relabel_nodes(G, dict(zip(range(4), ['sepal length (cm)', 'sepal width (cm)', 'petal length (cm)', 'petal width (cm)'])))
    
    # Color mapping
    color_palette = sns.cubehelix_palette(3)
    cmap = {k:color_palette[v-1] for k,v in zip(['sepal length (cm)', 'sepal width (cm)', 'petal length (cm)', 'petal width (cm)'],[2, 1, 3, 2])}
    
    # Draw
    nx.draw(G, node_color=[cmap[node] for node in G.nodes()], with_labels=True)
    

In this, they are all using `matplotlib` color palettes:
<http://jakevdp.github.io/mpl_tutorial/tutorial_pages/tut3.html> I even tried
converting them to a `ListedColormap` object but it didn't work.

This doesn't work for my situation either b/c matplotlib colormap: [Seaborn
regplot with colorbar?](http://stackoverflow.com/questions/30353363/seaborn-
regplot-with-colorbar)

Same for
<http://matplotlib.org/examples/pylab_examples/colorbar_tick_labelling_demo.html>

This was the closest I got but it didn't work I got a autoscale Nonetype: [How
do I use seaborns color_palette as a colormap in
matplotlib?](http://stackoverflow.com/questions/37902459/how-do-i-use-
seaborns-color-palette-as-a-colormap-in-matplotlib)


Answer: I think the best thing to do here is to fake it following [this
answer](http://stackoverflow.com/a/11558629/5285918) since you don't have a
"ScalarMappable" to work with.

For a discrete colormap

    
    
    from matplotlib.colors import ListedColormap
    sm = plt.cm.ScalarMappable(cmap=ListedColormap(color_palette),
                               norm=plt.Normalize(vmin=0, vmax=3))
    sm._A = []
    plt.colorbar(sm)
    

If you want a linear (continuous) colormap and to only show integer ticks

    
    
    sm = plt.cm.ScalarMappable(cmap=sns.cubehelix_palette(3, as_cmap=True),
                               norm=plt.Normalize(vmin=0, vmax=3))
    sm._A = []
    plt.colorbar(sm, ticks=range(4))
    

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

