Seaborn diagonal correlation matrix skip first row and last column

Viewed 1117

How can I drop the label and corresponding column/row (the white space) from the following plot? In this particular case in would correspond to row a and column l.

mask = np.zeros_like(corr, dtype=np.bool)
mask[np.triu_indices_from(mask)] = True

f, ax = plt.subplots(figsize=(12, 10))
cmap = sns.diverging_palette(220, 10, as_cmap=True)

corr = scaled_df.select_dtypes(include = ['float64', 'int64']).corr()

sns.heatmap(corr, mask=mask, cmap='inferno', vmin = -0.1, vmax=0.8, square=True)
plt.axvline(1.0, color='white', linestyle = '-', linewidth = 2.5, alpha = 1.0)

enter image description here

1 Answers

Here are two fairly easy ways to approach this:

A. pass the valid /desired subset of data to the plotter

corr = np.random.rand(10,10)
mask = np.zeros_like(corr, dtype=np.bool)
mask[np.triu_indices_from(mask)] = True

sns.heatmap(corr[1:,:-1], mask=mask[1:,:-1], cmap='inferno', vmin = -0.1, vmax=0.8, square=True)

B. after rendering, adjust the limits of the plot

ax = sns.heatmap(corr, mask=mask, cmap='inferno', vmin = -0.1, vmax=0.8, square=True)
ax.set_xlim(0,9)
ax.set_ylim(10,1)

It isn't clear whether you always have have triangular valid regions, but you could detect the limits of your matrices from the mask according to the rows that are all set in the mask and equivalently for the columns. But for valid regions deriving from triu (or tril) the limits are just 1 fron the start / end.

Related