RadRuss
RadRuss

Reputation: 524

How to set mapping to colors in seaborn.heatmap?

I'm trying to plot a correlation matrix with sns.heatmap(). Specifically, the code I run is

corr = train.corr()
sns.heatmap(corr, cmap = "RdBu_r")

This returns following image: correlation matrix

What I want is to 0 to correspond to white, positive correlations to be in shades of red and negative to be in shades of blue. I went through the documentation of seaborn, however, I wasn't able to figure it out.

Is there a way how to set this in seaborn?

Upvotes: 8

Views: 16370

Answers (2)

Axel Demborg
Axel Demborg

Reputation: 31

A more robust solution might be to use the center argument in heatmap to set the center for plotting divergent data.

A = np.random.normal(1, 3, [5,5])
sns.heatmap(A, center=0, cmap=sns.diverging_palette(220, 20, as_cmap=True))

Results in the following figure

Upvotes: 3

johnnyheineken
johnnyheineken

Reputation: 613

Try playing with vmin and vmax attribute, as mentioned here

Upvotes: 5

Related Questions