1

It seems from the documentation like it should be possible to set all colorbar properties in seaborn.clustermap or seaborn.heatmap. Like the ticks in this example.

But the following gives an error in the colorbar initialization.

g = sns.clustermap(np.random.rand(5,20), 
                   row_cluster=None, col_cluster=None, figsize=(8,3),
                   vmin = 0.25, vmax=1.0, cbar_kws={"aspect":50})

TypeError                                 Traceback (most recent call last)
<ipython-input-29-820904ac4602> in <module>()
      1 g = sns.clustermap(np.random.rand(5,20), 
      2                    row_cluster=None, col_cluster=None, figsize=(8,3),
----> 3                    vmin = 0.25, vmax=1.0, cbar_kws={"aspect":50})

~\AppData\Local\Continuum\Miniconda3\lib\site-packages\seaborn\matrix.py 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)
   1299                         row_cluster=row_cluster, col_cluster=col_cluster,
   1300                         row_linkage=row_linkage, col_linkage=col_linkage,
-> 1301                         **kwargs)

~\AppData\Local\Continuum\Miniconda3\lib\site-packages\seaborn\matrix.py in plot(self, metric, method, colorbar_kws, row_cluster, col_cluster, row_linkage, col_linkage, **kws)
   1140 
   1141         self.plot_colors(xind, yind, **kws)
-> 1142         self.plot_matrix(colorbar_kws, xind, yind, **kws)
   1143         return self
   1144 

~\AppData\Local\Continuum\Miniconda3\lib\site-packages\seaborn\matrix.py in plot_matrix(self, colorbar_kws, xind, yind, **kws)
   1115         heatmap(self.data2d, ax=self.ax_heatmap, cbar_ax=self.cax,
   1116                 cbar_kws=colorbar_kws, mask=self.mask,
-> 1117                 xticklabels=xtl, yticklabels=ytl, **kws)
   1118 
   1119         ytl = self.ax_heatmap.get_yticklabels()

~\AppData\Local\Continuum\Miniconda3\lib\site-packages\seaborn\matrix.py in heatmap(data, vmin, vmax, cmap, center, robust, annot, fmt, annot_kws, linewidths, linecolor, cbar, cbar_kws, cbar_ax, square, xticklabels, yticklabels, mask, ax, **kwargs)
    526     if square:
    527         ax.set_aspect("equal")
--> 528     plotter.plot(ax, cbar_ax, kwargs)
    529     return ax
    530 

~\AppData\Local\Continuum\Miniconda3\lib\site-packages\seaborn\matrix.py in plot(self, ax, cax, kws)
    290         # Possibly add a colorbar
    291         if self.cbar:
--> 292             cb = ax.figure.colorbar(mesh, cax, ax, **self.cbar_kws)
    293             cb.outline.set_linewidth(0)
    294             # If rasterized is passed to pcolormesh, also rasterize the

~\AppData\Local\Continuum\Miniconda3\lib\site-packages\matplotlib\figure.py in colorbar(self, mappable, cax, ax, use_gridspec, **kw)
   1862                 cax, kw = cbar.make_axes(ax, **kw)
   1863         cax._hold = True
-> 1864         cb = cbar.colorbar_factory(cax, mappable, **kw)
   1865 
   1866         self.sca(current_ax)

~\AppData\Local\Continuum\Miniconda3\lib\site-packages\matplotlib\colorbar.py in colorbar_factory(cax, mappable, **kwargs)
   1366         cb = ColorbarPatch(cax, mappable, **kwargs)
   1367     else:
-> 1368         cb = Colorbar(cax, mappable, **kwargs)
   1369 
   1370     cid = mappable.callbacksSM.connect('changed', cb.on_mappable_changed)

~\AppData\Local\Continuum\Miniconda3\lib\site-packages\matplotlib\colorbar.py in __init__(self, ax, mappable, **kw)
    944                 kw['alpha'] = mappable.get_alpha()
    945 
--> 946             ColorbarBase.__init__(self, ax, **kw)
    947 
    948     def on_mappable_changed(self, mappable):

TypeError: __init__() got an unexpected keyword argument 'aspect'

Which properties are actually allowed to be set? Is there another way to make the colorbar look better when the figure is not square? (probably aspect is not even what I want, probably I actually want to change the bounding box).

Victor Chubukov
  • 1,345
  • 1
  • 10
  • 18
  • So how do you want the plot to look like? "better" is really broad and aestetics depend on personal taste - I cannot know what you like "better". Do you care about a heatmap or a clustermap here? – ImportanceOfBeingErnest Dec 21 '17 at 14:56
  • When the figure has larger width than height, the colorbar gets squished such that the labels are not readable. I would like it to extend further down the height of the figure. If you run the example, it should be clear that it doesn't meet any reasonable aesthetic standard of "good" :) – Victor Chubukov Dec 21 '17 at 15:21
  • and that's a separate question from whether these properties really aren't allowed to be set (if so, the documentation is misleading) – Victor Chubukov Dec 21 '17 at 15:23
  • Yes, but the reason the colorbar is on the top is that the clustermap usually needs the space left to it for the clusters. That is why I was asking whether you need a heatmap or a clustermap. The reason for the arguments working or not also depends on which one to use. – ImportanceOfBeingErnest Dec 21 '17 at 15:26
  • Ah, I see now, you're right, it works with `heatmap` (though I still think docs are misleading). I actually don't need the clustering but I use the clustermap because it has the feature of `col_colors` and `row_colors` to put categorical colorboxes in. I suppose I could add that in manually, though it's a bit of a pain. And in general even for maps with clustering, it would be nice to be able to manipulate the colorbar. – Victor Chubukov Dec 21 '17 at 15:29
  • The approach is a different one. For clustermap, which produces its own figure, one would create the colorbar manually and place it according to some grid. For heatmap one could do the same, but also use some of the existing keyword arguments. At the moment the question just goes into all directions at once and a possible answer would give a complete tutorial about how seaborn interacts with matplotlib. – ImportanceOfBeingErnest Dec 21 '17 at 15:40
  • The original point was simply "here's a behavior that's supposed to be possible, but in fact doesn't work for me". I included the fact that I was happy to consider alternative approaches. – Victor Chubukov Dec 21 '17 at 15:49
  • If you create the colorbar manually (for a clustermap), you need to specify the color range. Re-computing it from the data seems like the wrong approach -- is there a property of the clustermap object that stores the information and that I could use when re-creating a new colorbar? – Victor Chubukov Dec 21 '17 at 15:51
  • But I'm not sure what the desired output should look like. As you say yourself `aspect` would not be the property to change. – ImportanceOfBeingErnest Dec 21 '17 at 16:04

0 Answers0