将数字添加到相关热图

Add numbers to Correlation Heatmap

我创建了这个热图来可视化一段时间内多列数据之间的相关性。我真的很喜欢热图,但我想将相关系数作为数字添加到每个字段中。有人知道我必须在代码中添加什么才能做到这一点吗?

def CorrMtx(ten_yr_bond_p4, dropDuplicates = True):

    # Exclude duplicate correlations by masking uper right values
    if dropDuplicates:    
        mask = np.zeros_like(corr_ten_yr_bond_p4, dtype=np.bool)
        mask[np.triu_indices_from(mask)] = True

    # Set background color / chart style
    sns.set_style(style = 'white')

    # Set up  matplotlib figure
    f, ax = plt.subplots(figsize=(11, 9))

    # Add diverging colormap from red to blue
    cmap = sns.diverging_palette(250, 10, as_cmap=True)

    # Draw correlation plot with or without duplicates
    if dropDuplicates:
        sns.heatmap(corr_ten_yr_bond_p4, mask=mask, cmap=cmap, 
                square=True,
                linewidth=.5, cbar_kws={"shrink": .5}, ax=ax)
    else:
        sns.heatmap(corr_ten_yr_bond_p4, cmap=cmap, 
                                    vmin=-1, vmax=1, center=0,
                square=True,
                linewidth=.5, cbar_kws={"shrink": .5}, ax=ax)


CorrMtx(corr_ten_yr_bond_p4, dropDuplicates = False)

您需要在 sns.heatmap

中传递 annot=True
sns.heatmap(corr_ten_yr_bond_p4, annot=True, mask=mask, cmap=cmap, 
            square=True,
            linewidth=.5, cbar_kws={"shrink": .5}, ax=ax)