Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reverse colorscales in seaborn heatmap

For diverging values seaborn by default seems to show big numbers in warm tone (orange) and small numbers in cold tone (blue).

If I need to switch color to opposite, to show big numbers in blue and small in orange, how to do so?

I've searched but haven't found a way.

sns.heatmap(flights, center=flights.loc["January", 1955])
like image 451
santoku Avatar asked Dec 23 '22 20:12

santoku


2 Answers

You can reverse all of the matplotlib colormaps by appending _r to the name, i.e., plt.cm.coolwarm vs plt.cm.coolwarm_r.

I believe seaborn uses a cubehelix colormap by default.

So you'd do:

from matplotlib import pyplot
import seaborn as sns

colormap = pyplot.cm.cubehelix_r
flights = sns.load_dataset('flights').pivot("month", "year", "passengers")
sns.heatmap(flights, cmap=colormap)
like image 90
Paul H Avatar answered Jan 10 '23 15:01

Paul H


There is no need to build a separate reverse function for heatmap.

Just use cmap = 'magma_r'. The default setting is magma and thus we just append the '_r' for reverse.

like image 42
Pranav Tumkur Avatar answered Jan 10 '23 15:01

Pranav Tumkur