设置群集图上等于零的值的特定颜色

我想对带有seaborn的群集地图的图形输出有所帮助.

在我的数据中,我缺少转换为0的数据.

我希望白色的值等于零,其余部分为调色板.

有没有办法在cmap中指示它?

import pandas as pd
from random import randint
import seaborn as sns
import matplotlib.pyplot as plt


df = pd.DataFrame({'A': [randint(1, 10) for x in xrange(10)]+[randint(30, 50) for x in xrange(5)]+[randint(70, 100) for x in xrange(5)],
         'B': [randint(0, 2) for x in xrange(10)]+[randint(30, 50) for x in xrange(5)]+[randint(70, 100) for x in xrange(5)],
         'C': [randint(0, 10) for x in xrange(10)]+[randint(30, 50) for x in xrange(5)]+[randint(60, 100) for x in xrange(5)],
         'D': [randint(0, 40) for x in xrange(10)]+[randint(30, 50) for x in xrange(5)]+[randint(60, 100) for x in xrange(5)]})

cmap = sns.cubehelix_palette(as_cmap=True, start=.5, rot=-.75, light=.9)

sns.clustermap(df, figsize=(13, 13), cmap=cmap)

实际集群:

结果值为白色的白色:

最佳答案

clustermap具有kwarg掩码.从docs开始:

mask : boolean array or DataFrame, optional

If passed, data will not be shown in cells where mask is True. Cells with missing values are automatically masked. Only used for visualizing, not for calculating.

因此,对于您的示例,您可以使用布尔数组,例如:mask =(df == 0)

sns.clustermap(df, figsize=(13, 13), cmap=cmap, mask=(df==0))

enter image description here