Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change color of missing values in Seaborn heatmap

Consider the example of missing values in the Seaborn documentation:

corr = np.corrcoef(np.random.randn(10, 200))
mask = np.zeros_like(corr)
mask[np.triu_indices_from(mask)] = True
sns.heatmap(corr, mask=mask, vmax=.3, square=True)

Heatmap

How do I change the color of the missing values to, for example, black? The color of the missing values should be specified independent of the color scheme of the heatmap, it may not be present in the color scheme.

I tried adding facecolor = 'black' but that didn't work. The color can be affected by e.g. sns.axes_style("white") but it isn't clear to me how that can be used to set an arbitrary color.

like image 948
C. E. Avatar asked Sep 19 '18 07:09

C. E.


People also ask

How do I change my color palette in Seaborn heatmap?

You can change the color of the seaborn heatmap by using the color map using the cmap attribute of the heatmap.

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

How do I find missing values in Seaborn?

The key function for both the approaches to visualize missing data is to use Pandas isna() function to find if each element in the dataframe is a missing value or not. By using isna() on Pandas dataframe, we get a boolean dataframe with True for missing data and False for the NOT missing data.

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.


2 Answers

You can use the following code:

corr = np.corrcoef(np.random.randn(10, 200))
mask = np.zeros_like(corr)
mask[np.triu_indices_from(mask)] = True
g = sns.heatmap(corr, mask=mask, vmax=.3, square=True)
g.set_facecolor('xkcd:salmon')

You need to use set_facecolor on the plot object. Change to any colour you want.

Resulting in this graph:

enter image description here

like image 153
error Avatar answered Oct 30 '22 05:10

error


Another alternative would be to set the active style parameters in seaborn using sns.set_style():

sns.set_style("white",  {'figure.facecolor': 'black'})
corr = np.corrcoef(np.random.randn(10, 200))
mask = np.zeros_like(corr)
mask[np.triu_indices_from(mask)] = True
sns.heatmap(corr, mask=mask, vmax=.3, square=True)
plt.axis("off")
plt.show()

An entire list of parameters can be found here: https://seaborn.pydata.org/tutorial/aesthetics.html#overriding-elements-of-the-seaborn-styles

like image 32
Kevinj22 Avatar answered Oct 30 '22 05:10

Kevinj22