Skip to content
Advertisement

How to provide axis for seaborn clustermap

I want to provide the plot axes for a seaborn clustermap. The docs say that additional arguments are passed to the heatmap function. The docs of the heatmap function mention the keyword argument ax. Which is why I call the clustermap function with the keyword argmument ax=plt.gca(). However this will result in the following error:

  1182         # Setting ax_cbar=None in clustermap call implies no colorbar
   1183         kws.setdefault("cbar", self.ax_cbar is not None)
-> 1184         heatmap(self.data2d, ax=self.ax_heatmap, cbar_ax=self.ax_cbar,
   1185                 cbar_kws=colorbar_kws, mask=self.mask,
   1186                 xticklabels=xtl, yticklabels=ytl, annot=annot, **kws)

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

Thus I was wondering how to correctly pass the axis argument to the clustermap function?

Below is a minimal example to reproduce the error.

import matplotlib.pyplot as plt
import seaborn as sns; sns.set_theme(color_codes=True)

iris = sns.load_dataset("iris")

species = iris.pop("species")
plt.figure()
g = sns.clustermap(iris, ax=plt.gca())
plt.show()

PS.: Without the additional ax argument the plot works fine.

Advertisement

Answer

This is not possible as seaborn.clustermap is a figure level plotting function. There is one axes for the heat map and one for each tree.

What exactly would you like to achieve? You can always create your clustermap first and then modify it to add other axes.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement