Issue
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.
Solution
As pointed in the comments, using square=False
with figsize=(30, 15)
fixed the problem.
Answered By - Matheus Torquato
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.