Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing the size of the heatmap specifically in a seaborn clustermap?

I'm making a clustered heatmap in seaborn as follows

import numpy as np
import seaborn as sns
np.random.seed(2)
data = np.random.randn(100, 10)
sns.clustermap(data)

but the rows are squished:

enter image description here

but if I pass a size to the clustermap function then it looks terrible

enter image description here

is there a way to only increase the size of the heatmap part? So that the row names can be read, but not stretch out the cluster portions.

like image 837
tlnagy Avatar asked Oct 15 '15 00:10

tlnagy


People also ask

How do you adjust heatmap size?

The font size of the annotations is set by default, although it can be altered using the annot kws parameter of the heatmap() method. The annot kws is a dictionary-type option that requires a value for the size key. The size of the annotations is determined by the value assigned to this key.

What is cluster map in Seaborn?

The clustermap() function of seaborn plots a hierarchically-clustered heat map of the given matrix dataset. It returns a clustered grid index.


1 Answers

As @mwaskom commented, I was able to use ax_heatmap.set_position along with the get_position function to achieve the correct result.

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

np.random.seed(2)
data = np.random.randn(100, 10)
cm = sns.clustermap(data)
hm = cm.ax_heatmap.get_position()
plt.setp(cm.ax_heatmap.yaxis.get_majorticklabels(), fontsize=6)
cm.ax_heatmap.set_position([hm.x0, hm.y0, hm.width*0.25, hm.height])
col = cm.ax_col_dendrogram.get_position()
cm.ax_col_dendrogram.set_position([col.x0, col.y0, col.width*0.25, col.height*0.5])

enter image description here

like image 165
tlnagy Avatar answered Oct 22 '22 02:10

tlnagy