Seaborn clustermap colorbar adjustment

Viewed 1613

Having several issues adjusting the colorbar in seaborn.clustermap. I'm trying to:

  1. orient the colorbar horizontally
  2. change the colorbar border color
  3. change the colorbar tick length

I've checked the documentation for figure.colorbar to no avail.

Minimal code:

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

#data
corr = np.corrcoef(np.random.randn(10, 200))

#clustermap
kws = dict(cbar_kws=dict(label='Label', ticks=[0,0.50,1], orientation='horizontal'), figsize=(6, 6))
sns.clustermap(corr, cmap="Blues", xticklabels=False, yticklabels=False, **kws)
plt.show()

enter image description here

1 Answers

You can grab the subplot with the colorbar via g.ax_cbar. Then you can change its position, title, spines and tick lengths:

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

corr = np.corrcoef(np.random.randn(10, 200))

kws = dict(cbar_kws=dict(ticks=[0, 0.50, 1], orientation='horizontal'), figsize=(6, 6))
g = sns.clustermap(corr, cmap="Blues", xticklabels=False, yticklabels=False, **kws)
x0, _y0, _w, _h = g.cbar_pos
g.ax_cbar.set_position([x0, 0.9, g.ax_row_dendrogram.get_position().width, 0.02])
g.ax_cbar.set_title('colorbar title')
g.ax_cbar.tick_params(axis='x', length=10)
for spine in g.ax_cbar.spines:
    g.ax_cbar.spines[spine].set_color('crimson')
    g.ax_cbar.spines[spine].set_linewidth(2)
plt.show()

sns.clustermap with customized colorbar

Related