2

I'm trying to plot a triangular correlation matrix using seaborn heatmap but the cells won't fit the annotation digits.

Any idea how I make them fit nicely inside their respective heatmap cell?

I already tried changing the figsize and that did not help. Also tried using square=False.

I'm using seaborn==0.11.2 and matplotlib==3.4.3

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

# Generate a dummy df
df = pd.DataFrame(np.random.rand(44,44))

label_lens = [16, 16, 16, 16, 16, 16, 16, 16, 20, 11,
              9, 10, 10, 16, 16, 16, 16, 12, 45, 10, 10,
             10, 10, 10, 10, 10, 10, 12, 12, 50, 50, 50,
             50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50]

col_labels = []
for label_len in label_lens:
    col_labels.append('X'*label_len)

df.columns = col_labels

# Build correlation matrix df
correlation_matrix = df.corr()

# Get Diagonal Mask. Square matrix is not relevant.
mask = np.triu(np.ones_like(correlation_matrix, dtype=bool))

# Set up the matplotlib figure
f, ax = plt.subplots(figsize=(30, 15))

# Draw the heatmap with the mask and correct aspect ratio
sns_plot = sns.heatmap(correlation_matrix,
                       mask=mask,
                       annot=True,
                       fmt='.2f',
                       square=True)
f.set_tight_layout(True)
f.savefig("my_corr_matrix.pdf")

I replaced my labels here with placeholders of the same sizes as the actual labels.

enter image description here

Matheus Torquato
  • 1,293
  • 18
  • 25

2 Answers2

2

As pointed in the comments, using square=False with figsize=(30, 15) fixed the problem.

Matheus Torquato
  • 1,293
  • 18
  • 25
-2

The issue is that your labels are taking too much space of your jupyter max width. You probably have 2 options, the first one is editing jupyter width according to this answer:

https://stackoverflow.com/a/34058270/2970272

The second one would be lowering how much you're using the plot space, by trimming your labels, lowering the font, removing the colorbar and such. From the code bellow check all the "## HERE" comments to easily find what I've changed.

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

# Generate a dummy df
df = pd.DataFrame(np.random.rand(44,44))

label_lens = [16, 16, 16, 16, 16, 16, 16, 16, 20, 11,
              9, 10, 10, 16, 16, 16, 16, 12, 45, 10, 10,
             10, 10, 10, 10, 10, 10, 12, 12, 50, 50, 50,
             50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50]
#label_lens= [5]*len(label_lens)

col_labels = []
for label_len in label_lens:
    col_labels.append(f"{'X'*label_len}"[:10]) ## HERE -- max char limit

df.columns = col_labels

# Build correlation matrix df
correlation_matrix = df.corr()

# Get Diagonal Mask. Square matrix is not relevant.
mask = np.triu(np.ones_like(correlation_matrix, dtype=bool))

# Set up the matplotlib figure
f, ax = plt.subplots(figsize=(30, 20)) ## here increased figsize

## HERE - max tick font size
ax.tick_params(axis='both', labelsize=8)

# Draw the heatmap with the mask and correct aspect ratio
sns_plot = sns.heatmap(correlation_matrix,
                       mask=mask,
                       annot=True,
                       fmt='.2f',
                       square=True,
                       cbar=False) ## HERE removed colorbar
f.set_tight_layout(True)
## HERE - rotating ticks to give more space
plt.setp(ax.yaxis.get_majorticklabels(), rotation=-45)
plt.setp(ax.xaxis.get_majorticklabels(), rotation=-45)
plt.show()
#f.savefig("my_corr_matrix.pdf")
mrbTT
  • 1,399
  • 1
  • 18
  • 31