How to apply grid only on lower triangular area of a heatmap?

63 Views Asked by At

I created a heatmap and masked out the upper triangular portion of it. But when I am applying line width to it, grid is appearing to the whole portion of the heatmap. But I want mask out the upper section. Can anyone help me with it?

My code,

import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sb
import numpy as np
import seaborn as sns

plt.figure(figsize=(10,10))

# importing Dataset
data_df = pd.read_csv("My data.csv")

#corr matrix
r=data.corr()
r[abs(r)<0.00001]=0
r.round(decimals=4, out=None)



params = {'mathtext.default': 'regular' }
plt.rcParams.update(params)


# mask
mask=np.ones([7,7])
mask *= 1-np.tri(*mask.shape,k=0)

# plotting a masked correlation heatmap
dataplot = sb.heatmap(r, cmap="gist_heat", vmin=-0.2, vmax=1.1,annot=True, mask=mask,fmt='.4f',annot_kws={"fontsize":15},linewidths=1, linecolor='black',cbar_kws={"shrink": 0.81}, clip_on=False,square=True)

plt.xticks(rotation=90)
plt.yticks(rotation=0)

plt.savefig('/content/heatmap.png',bbox_inches = 'tight',dpi=330)

# displaying heatmap
plt.show()

What I am getting:

enter image description here

What I want: enter image description here

1

There are 1 best solutions below

0
rorodejuarez On

The linewidth parameter is not the one that's making the grid lines to appear on the whole correlation matrix, it specifies how much should each cell be separated from one another. The parameter linecolor is the one that displays the grid lines on all the matrix. If you drop it, the resulting matrix should be the one that you are looking for. You can check further details in official seaborn documentation on how to plot a diagonal correlation matrix.