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

<p><strong>How can I add a color bar type feature on a networkx plot using a seaborn color palette?</strong> </p>

<p><a href="http://i.stack.imgur.com/Yt5ud.png" rel="nofollow"><img src="http://i.stack.imgur.com/Yt5ud.png" alt="enter image description here"></a></p>

<pre><code># 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)
</code></pre>

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

<p>This doesn't work for my situation either b/c matplotlib colormap: <a href="http://stackoverflow.com/questions/30353363/seaborn-regplot-with-colorbar">Seaborn regplot with colorbar?</a></p>

<p>Same for <a href="http://matplotlib.org/examples/pylab_examples/colorbar_tick_labelling_demo.html" rel="nofollow">http://matplotlib.org/examples/pylab_examples/colorbar_tick_labelling_demo.html</a></p>

<p>This was the closest I got but it didn't work I got a autoscale Nonetype: <a href="http://stackoverflow.com/questions/37902459/how-do-i-use-seaborns-color-palette-as-a-colormap-in-matplotlib">How do I use seaborns color_palette as a colormap in matplotlib?</a></p>

<p>I think the best thing to do here is to fake it following <a href="http://stackoverflow.com/a/11558629/5285918">this answer</a> since you don't have a "ScalarMappable" to work with.</p>

<p>For a discrete colormap</p>

<pre><code>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)
</code></pre>

<p>If you want a linear (continuous) colormap and to only show integer ticks</p>

<pre><code>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))
</code></pre>

<p><a href="http://i.stack.imgur.com/pswBu.png" rel="nofollow"><img src="http://i.stack.imgur.com/pswBu.png" alt="enter image description here"></a></p>

python matplotlib colors networkx seaborn