Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change color of certain squares in a seaborn heatmap?

I'm trying to create a heatmap in seaborn (python) with certain squares colored with a different color, (these squares contain insignificant data - in my case it will be squares with values less than 1.3, which is -log of p-values >0.05). I couldn't find such function. Masking these squares also didn't work. Here is my code:

import matplotlib.pyplot as plt
import numpy as np
import matplotlib as mpl
import seaborn as sns; sns.set()
data = [[1.3531363408, 3.339479161, 0.0760855365], [5.1167382617, 3.2890920405, 2.4764601828], [0.0025058257, 2.3165128345, 1.6532714962], [0.2600549869, 5.8427407219, 6.6627226609], [3.0828581725, 16.3825494439, 12.6722666929], [2.3386307357, 13.7275065772, 12.5760972276], [1.224683813, 2.2213656372, 0.6300876451], [0.4163788387, 1.8128374089, 0.0013106046], [0.0277592882, 2.9286203949, 0.810978992], [0.0086613622, 0.6181261247, 1.8287878837], [1.0174519889, 0.2621290291, 0.1922637697], [3.4687429571, 4.0061981716, 0.5507951444], [7.4201304939, 3.881457516, 0.1294141768], [2.5227546319, 6.0526491816, 0.3814362442], [8.147538027, 14.0975727815, 7.9755706939]]
cmap2 = mpl.colors.ListedColormap(sns.cubehelix_palette(n_colors=20, start=0, rot=0.4, gamma=1, hue=0.8, light=0.85, dark=0.15, reverse=False))
ax = sns.heatmap(data, cmap=cmap2, vmin=0)
plt.show()

I want to add that I'm not very advanced programmer.

like image 329
Ilona Avatar asked Oct 14 '15 12:10

Ilona


People also ask

How do I change the color of my Seaborn 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.

What is CMAP in Seaborn?

Inside the variable data, we call a NumPy function rand which set the number limit for both the axes in the plot. Then, we have a Seaborn heatmap function, which takes the argument cmap. The cmap is set with the default color scheme which is the coolwarm colors.

What is FMT in Seaborn heatmap?

The seaborn heatmap fmt help to show annot with different formatting.


1 Answers

OK, so I can answer my question myself now :) Here is the code that solved the problem:

import matplotlib.pyplot as plt
import numpy as np
import matplotlib as mpl
import seaborn as sns; sns.set()

data = np.array([[1.3531363408, 3.339479161, 0.0760855365],
                 [5.1167382617, 3.2890920405, 2.4764601828],
                 [0.0025058257, 2.3165128345, 1.6532714962],
                 [0.2600549869, 5.8427407219, 6.6627226609],
                 [3.0828581725, 16.3825494439, 12.6722666929],
                 [2.3386307357, 13.7275065772, 12.5760972276],
                 [1.224683813, 2.2213656372, 0.6300876451],
                 [0.4163788387, 1.8128374089, 0.0013106046],
                 [0.0277592882, 2.9286203949, 0.810978992],
                 [0.0086613622, 0.6181261247, 1.8287878837],
                 [1.0174519889, 0.2621290291, 0.1922637697],
                 [3.4687429571, 4.0061981716, 0.5507951444],
                 [7.4201304939, 3.881457516, 0.1294141768],
                 [2.5227546319, 6.0526491816, 0.3814362442],
                 [8.147538027, 14.0975727815, 7.9755706939]])
cmap1 = mpl.colors.ListedColormap(['c'])

fig, ax = plt.subplots(figsize=(8, 8))
sns.heatmap(data, ax=ax)
sns.heatmap(data, mask=data > 1.3, cmap=cmap1, cbar=False, ax=ax)
plt.show()

enter image description here

So the problem with masking which didn't work before was that it works only on arrays not on lists. And another thing is just plotting the heatmap twice -second time with masking. The only thing I still don't understand is that it masks opposite fields from what is written.. I want to mask values below 1.3, but then it colored values above 1.3.. So I wrote mask=data >1.3 and now it works...

like image 78
Ilona Avatar answered Oct 17 '22 18:10

Ilona