Home > front end >  Seaborn Heatmap correlation won't fit annotation digits
Seaborn Heatmap correlation won't fit annotation digits

Time:11-12

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

CodePudding user response:

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

CodePudding user response:

It will be kinda hard considering the fontsize... you could try rotating your labels, with something like

plt.setp(ax.xaxis.get_majorticklabels(), rotation=25)
  • Related