如何使用`seaborn`调色板将`colorbar`添加到`networkx`? (Python 3)

时间:2016-09-21 22:26:00

标签: python matplotlib colors networkx seaborn

我尝试在colorbar(最轻)和networkx范围内向matplotlib ax 1 3添加cmap (是最黑暗的)[查看下面PyData的行。我试图结合很多# 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) 功能。

如何使用seaborn调色板在networkx图上添加颜色条类型功能?

enter image description here

matplotlib

在此,他们都使用ListedColormap调色板:http://jakevdp.github.io/mpl_tutorial/tutorial_pages/tut3.html我甚至尝试将它们转换为void zero_row (int a [4][5], int row){ for (int j = 0; j < 5; j++) { // 5 instead of 4 a[row][j] = 0; } } 对象,但它没有用。

这对我的情况不起作用b / c matplotlib colormap:Seaborn regplot with colorbar?

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

相同

这是我得到的最接近但是它没有工作我得到了自动缩放类型:How do I use seaborns color_palette as a colormap in matplotlib?

1 个答案:

答案 0 :(得分:3)

我认为最好的办法就是在this answer之后伪造它,因为你没有“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