Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to invert color of seaborn heatmap colorbar

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

like image 306
Fed Avatar asked Nov 23 '17 18:11

Fed


People also ask

How do you reverse the color bar in Python?

We can reverse the colormap of the plot with the help of two methods: By using the reversed() function to reverse the colormap. By using “_r” at the end of colormap name.

How to remove the color bar heatmap?

You can remove the color bar from a heatmap plot by giving False to the parameter cbar .

How do you color heatmap?

You can customize the colors in your heatmap with the cmap parameter of the heatmap() function in seaborn. The following examples show the appearences of different sequential color palettes.


2 Answers

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) 
like image 180
Ben Avatar answered Sep 21 '22 10:09

Ben


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

like image 33
benbo Avatar answered Sep 21 '22 10:09

benbo