How to invert color of seaborn heatmap colorbar

Fed picture Fed · Nov 23, 2017 · Viewed 20.8k times · Source

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

Answer

Ben picture Ben · Nov 24, 2017

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)