How to invert color of seaborn heatmap colorbar

雨燕双飞 提交于 2020-02-17 05:53:08

问题


I use an heatmap to visualize a confusion matrix. I like the standard colors, but I would like to have 0s in light orange and highest values in dark purple.

I managed to do so only with another set of colors (light to dark violet), setting:

colormap = sns.cubehelix_palette(as_cmap=True)
ax = sns.heatmap(cm_prob, annot=False, fmt=".3f", xticklabels=print_categories, yticklabels=print_categories, vmin=-0.05, cmap=colormap)

But I want to keep these standard ones. This is my code and the image I get.

ax = sns.heatmap(cm_prob, annot=False, fmt=".3f", xticklabels=print_categories, yticklabels=print_categories, vmin=-0.05)


回答1:


The default cmap is sns.cm.rocket. To reverse it set cmap to sns.cm.rocket_r

Using your code:

cmap = sns.cm.rocket_r

ax = sns.heatmap(cm_prob,
                 annot=False,
                 fmt=".3f",
                 xticklabels=print_categories,
                 yticklabels=print_categories,
                 vmin=-0.05,
                 cmap = cmap)



回答2:


To expand on Ben's answer, you can do this with most if not any color map.

import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
X = np.random.random((4, 4))
sns.heatmap(X,cmap="Blues")
plt.show()
sns.heatmap(X,cmap="Blues_r")
plt.show()
sns.heatmap(X,cmap="YlGnBu")
plt.show()
sns.heatmap(X,cmap="YlGnBu_r")
plt.show()




回答3:


Did you try to invert the colormap?

sns.cubehelix_palette(as_cmap=True, reverse=True)


来源:https://stackoverflow.com/questions/47461506/how-to-invert-color-of-seaborn-heatmap-colorbar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!