How to add `colorbar` to` networkx` using the `seaborn` palette? (Python 3)

I am trying to add colorbarto my networkxdraw matplotlib axfrom a range 1(being the lightest) and 3(being the darkest) [check w / cmapbelow line]. I am trying to combine many features PyData.

How to add a function like a color bar on a networkx graph using a palette with sea colors?

enter image description here

# 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 all use color palettes matplotlib: http://jakevdp.imtqy.com/mpl_tutorial/tutorial_pages/tut3.html I even tried to convert them to an object ListedColormapbut it didn’t work.

This does not work for my situation either b / c matplotlib colormap: Marine regplot with color bar

http://matplotlib.org/examples/pylab_examples/colorbar_tick_labelling_demo.html

, . Nonetype: seaborns color_palette matplotlib?

+4
1

, , , - , "ScalarMappable" .

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)

()

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

+3

Source: https://habr.com/ru/post/1655403/


All Articles