Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

seaborn clustermap: set colorbar ticks

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?

like image 323
Dima Lituiev Avatar asked May 19 '17 18:05

Dima Lituiev


1 Answers

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

like image 136
ImportanceOfBeingErnest Avatar answered Nov 04 '22 02:11

ImportanceOfBeingErnest