6

I would like to modify the ticks of a colorbar in a seaborn.clustermap. This answer addresses this question for a general matplotlib colorbar.

g = sns.clustermap(np.random.rand(20,20), 
                   row_cluster=None, col_cluster=None,
                   vmin = 0.25, vmax=1.0)

For some reason when I specify clustermap(..., vmin=0.25, vmax=1.0), I get ticks from 0.3 to 0.9, but no 1.0. If I extend vmax=1.05, I get a tick precisely at 1.05.

My guess was that the g.cax property of the object returned by clustermap is the colorbar, but it has no .set_ticks() method.

Any ideas how one can I set the ticks?

Community
  • 1
  • 1
Dima Lituiev
  • 12,544
  • 10
  • 41
  • 58
  • 2
    Your question is clear but it would be best if you wrote out an example such that someone could easily copy and paste it to start helping you. – mwaskom May 19 '17 at 18:26

1 Answers1

8

Just like seaborn.heatmap the seaborn.clustermap has an argument cbar_kws (colorbar keyword arguments). This expects a dictionary of possible arguments to the matplotlib colorbar function. Because with matplotlib, we would use the ticks argument to colorbar in order to set the ticks manually, we can provide a dictionary like this

g = sns.clustermap(..., cbar_kws={"ticks":[0.25,1]})

to obtain the tick marks at 0.25 and 1 in the colorbar. (The list can of course be extended, if you want more tickmarks.)

Complete code:

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

g = sns.clustermap(np.random.rand(20,20), 
                   row_cluster=None, col_cluster=None,
                   vmin = 0.25, vmax=1.0, cbar_kws={"ticks":[0.25,1]})

plt.show()

enter image description here

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712