47

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)

enter image description here

Fed
  • 609
  • 3
  • 7
  • 13

5 Answers5

72

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)
Ben
  • 958
  • 6
  • 7
  • for me I have to use `plt.cm` as `sns` does't have `cm` module. – Amir Imani Aug 15 '18 at 19:34
  • @AmirhosImani what version of seaborn are you using? It seems like it's still part of the current version (see [here](https://github.com/mwaskom/seaborn/tree/master/seaborn)) – Ben Aug 22 '18 at 23:25
31

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()

enter image description here

benbo
  • 1,471
  • 1
  • 16
  • 29
5
  • only add cmap="rocket_r" to sns.heatmap
    • cmap="rocket": is the default palette of heatmap
    • add_r: to reverse the colors of palette
ax = sns.heatmap(cm_prob, annot=False, fmt=".3f", xticklabels=print_categories, yticklabels=print_categories, vmin=-0.05,cmap="rocket_r")
Nourhan_selim
  • 101
  • 1
  • 1
2

Did you try to invert the colormap?

sns.cubehelix_palette(as_cmap=True, reverse=True)
ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
  • Yep, but, as I already wrote in the question, I want those same standard colors not the ones in cubehelix_palette (which has shades of purple without orange) – Fed Nov 24 '17 at 22:02
  • I may have misunderstood the question then. Maybe the other answer is what you are looking for? – ImportanceOfBeingErnest Nov 25 '17 at 11:58
2

we can now quickly achieve reverse color just by putting _r in the end.

For example: for viridis => viridis_r

sns.heatmap(corr_matrix, annot=True, cmap='viridis_r');
kitokid
  • 3,009
  • 17
  • 65
  • 101