Answer a question

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.

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

Logo

Python社区为您提供最前沿的新闻资讯和知识内容

更多推荐