1

I created this heatmap to visualise the correlations between multiple columns of data in a period of time. I really like the heatmap, but I want to add the correleation coefficients as a number into each of the fields. Anyone know what I have to add in the code to do that?

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)

enter image description here

1 Answer 1

2

You need to pass annot=True in the sns.heatmap

sns.heatmap(corr_ten_yr_bond_p4, annot=True, mask=mask, cmap=cmap, 
            square=True,
            linewidth=.5, cbar_kws={"shrink": .5}, ax=ax)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.