Search code examples
pythonseabornhierarchical-clustering

seaborn change clustermap visualization options without redoing the clustering


Is it possible to run seaborn.clustermap on a previously obtained ClusterGrid object?

For example I user clustermap to obtain g in the following example:

import seaborn as ns
data = sns.load_dataset("iris")
species = iris.pop("species")

g = sns.clustermap(
    data, 
    cmap="mako",
    col_cluster=False,
    yticklabels=False, figsize=(5, 10),
    method='ward',
    metric="euclidean"
)

I would like to try different visualization options like different colormaps, figure sizes, how it looks with and without labels etc.

With the iris dataset everything is really fast, but I have a way larger dataset and the clustering part takes a lot of time.

Can I use g to show the heatmap and dendrogram using different options?


Solution

  • the object returned by clustermap is of type ClusterGrid. That object is not really documented in seaborn, however, it is essentially just a container for a few Axes objects. Depending on the kind of manipulations you want to make, you may simply need to access the relevant Axes object or the figure itself:

    # change the figure size after the fact
    g.fig.set_size_inches((4,4))
    # remove the labels of the heatmap
    g.ax_heatmap.set_xticklabels([])
    

    The colormap thing is a little more difficult to access. clustermap uses matplotlib pcolormesh under the hood. This function returns a collection object (QuadMesh), which is store in the list of collections of the main axes (g.ax_heatmap.collections). Since, AFAIK, seaborn doesn't plot anything else on that axes, We can get the QuadMesh object by its index [0], and then we can use any function applicable to that object.

    # change the colormap used
    g.ax_heatmap.collections[0].set_cmap('seismic')