2

so I plotted the cluster map of correlation and I found a few setbacks, if you guys could help me.

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

grafico_correlacao_renda_variavel_long_biased = sns.clustermap(correlacao_renda_variavel_long_biased, linewidths=.5,figsize=(40,40),annot= True,mask=mask,annot_kws={"size": 25})
plt.setp(grafico_correlacao_renda_variavel_long_biased.ax_heatmap.get_yticklabels(), rotation=0,fontsize=45)
plt.setp(grafico_correlacao_renda_variavel_long_biased.ax_heatmap.get_xticklabels(), rotation=90,fontsize=45)
grafico_correlacao_renda_variavel_long_biased.fig.suptitle('Renda Variável Long Biased',fontsize=100) 
grafico_correlacao_renda_variavel_long_biased.savefig('teste.pdf')
plt.show() 

enter image description here

So what are my problems:

1) the top and bottom are displaying only half of its size, and I don't know hot to fix it, I tried reducing the size but it did nothing to it.

2) Although I want the map to be clustered, I don't want the lines outside the map linking each "cell", is there a way to remove it ?

3) I want to show only the lower part of the diagonal matrix, but when I apply the mask, it gives me several blank spaces across the matrix

my mask: mask = np.zeros_like(correlacao_renda_variavel_long_biased, dtype=np.bool) mask[np.triu_indices_from(mask)] = True

enter image description here

khouzam
  • 263
  • 3
  • 14
  • 1) See [matplotlib-seaborn-first-and-last-row-cut-in-half-of-heatmap-plot](https://stackoverflow.com/questions/56942670/matplotlib-seaborn-first-and-last-row-cut-in-half-of-heatmap-plot) 2) Is probably due to `edgecolor` not being set. 3) Is impossible to know, see [mcve]. – ImportanceOfBeingErnest Nov 12 '19 at 17:20
  • thank you for your time, does it helps know ? – khouzam Nov 12 '19 at 17:23

1 Answers1

4

It seems like the problem is that the clustermap is permuting the mask together with the data. My solution to this was to run the clustermap twice: once to find out the permutations in order to create a mask such that once you apply the permutation to it, you recover the mask you actually want. Then, run the clustermap with this new mask. That is:

g = grafico_correlacao_renda_variavel_long_biased #for conciseness
# apply the inverse permutation to the mask
mask = mask[np.argsort(g.dendrogram_row.reordered_ind),:]
mask = mask[:,np.argsort(g.dendrogram_col.reordered_ind)]
# run the clustermap again with the new mask
grafico_correlacao_renda_variavel_long_biased = sns.clustermap(correlacao_renda_variavel_long_biased, linewidths=.5,figsize=(40,40),annot= True,mask=mask,annot_kws={"size": 25})

I hope this doesn't come too late. I just had the exact same problem and found your question.

GermanK
  • 1,676
  • 2
  • 14
  • 21
  • can you update this solution with complete minimal code including a simple DataFrame or something like `corr = np.corrcoef(np.random.randn(10, 200))`? – Cactus Philosopher Jun 08 '21 at 20:31