Seaborn: How to remove colorbar from clustermap?

Created on 15 Mar 2015  路  10Comments  路  Source: mwaskom/seaborn

Hi all,
I cannot find a way to remove a colorbar from a clustermap. Is that missing by design?
Thanks

annoyance question

Most helpful comment

just figured out how to remove the colorbar

cm = sns.clustermap(data)
cm.cax.set_visible(False)

All 10 comments

Yes, it was intentionally kept there so the scale of the colors is explicit. To remove it, you can create a separate ax object, and feed that to the cbar_ax argument.

cbar_ax = plt.gca()
sns.clustermap(data, cbar_ax=cbar_ax)

Unfortunately I got:

TypeError                                 Traceback (most recent call last)
    <ipython-input-14-8de4a18053a2> in <module>()
      1 plt.figure(1)
      2 cbar_ax = plt.gca()
----> 3 sns.clustermap(results_anova, row_cluster=False, col_colors=col_colors, figsize=(samples/5, act/5), cbar_ax=cbar_ax)
      4 plt.savefig("heatmap_cluster.svg", bbox_inches="tight")
      5 plt.savefig("heatmap_cluster.pdf", bbox_inches="tight")

/usr/local/lib/python2.7/dist-packages/seaborn/matrix.pyc in clustermap(data, pivot_kws, method, metric, z_score, standard_scale, figsize, cbar_kws, row_cluster, col_cluster, row_linkage, col_linkage, row_colors, col_colors, mask, **kwargs)
    893                         row_linkage=row_linkage, col_linkage=col_linkage,
    894                         mask=mask,
--> 895                         **kwargs)

/usr/local/lib/python2.7/dist-packages/seaborn/matrix.pyc in plot(self, metric, method, colorbar_kws, row_cluster, col_cluster, row_linkage, col_linkage, mask, **kws)
    811                               row_linkage=row_linkage, col_linkage=col_linkage)
    812         self.plot_colors(**kws)
--> 813         self.plot_matrix(colorbar_kws, mask, **kws)
    814         return self
    815 

/usr/local/lib/python2.7/dist-packages/seaborn/matrix.pyc in plot_matrix(self, colorbar_kws, mask, **kws)
    801         self.data2d = self.data2d.iloc[yind, xind]
    802         heatmap(self.data2d, ax=self.ax_heatmap, cbar_ax=self.cax,
--> 803                 cbar_kws=colorbar_kws, mask=mask, **kws)
    804         self.ax_heatmap.yaxis.set_ticks_position('right')
    805         self.ax_heatmap.yaxis.set_label_position('right')

TypeError: heatmap() got multiple values for keyword argument 'cbar_ax'

Is there any reason why the colorbar Axes doesn't end up as an attribute of the ClusterGrid like the other components of the figure?

I don't know. In case it is useful, I am using seaborn v0.5.1 on Ubuntu 14.04.

Is there any reason why the colorbar Axes doesn't end up as an attribute of the ClusterGrid like the other components of the figure?

No, that was oversight on my part. I must have forgotten about that one.

Yes, it was intentionally kept there so the scale of the colors is explicit.

For numeric data this makes sense. For binary data, though, colorbars are not only redundant, but potentially confusing as they may mislead there is a scale where there isn't. An option to disable them would be nice.

I also can reproduce the cbar_ax issue described above by @fbrundu

just figured out how to remove the colorbar

cm = sns.clustermap(data)
cm.cax.set_visible(False)

@hanice I've trying your solution:

grid = seaborn.clustermap(heatmap_df, row_cluster=False,
    col_cluster=False, row_colors=status_colors, figsize=(4, 5),
    linewidths=1)
grid.cax.set_visible(False)
plt.setp(grid.ax_heatmap.get_yticklabels(), rotation=0);
grid.ax_heatmap.set_ylabel("");
grid.savefig('figure/example-feature-matrix.png', dpi=300, bbox_inches='tight', transparent=True)

I get the following:

example-feature-matrix

Notice the gap! I've been unable to find a way to remove the whitespace/margin where the legend used to be. Any ideas?

@dhimmel You might have solved this issue already but for what it's worth i'll put my solution here.
To control the top dendrogram axis position and size I use the following:

col = grid.ax_col_dendrogram.get_position()
grid.ax_col_dendrogram.set_position([col.x0, col.y0, col.width, col.height*0.01])

there should be the same for the left by using row instead of col in ax_col_. This feels like a workaround but it seems to work at least.

@dhimmel and @Xparx

A more updated solution is probably to set visible to False for ax_row_dendrogram and/or ax_col_dendrogram:

g = clustermap(...)
g.cax.set_visible(False)
g.ax_row_dendrogram.set_visible(False)
g.ax_col_dendrogram.set_visible(False)
Was this page helpful?
0 / 5 - 0 ratings

Related issues

vinay-jayaram picture vinay-jayaram  路  3Comments

wenhaosun picture wenhaosun  路  3Comments

queryous picture queryous  路  4Comments

bondarevts picture bondarevts  路  3Comments

tritemio picture tritemio  路  3Comments