Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add a Title to a Seaborn Clustermap?

I have searched, but not yet discovered how to add a title to a Seaborn Clustermap.

I have tried:

plt.title('Title',loc='center')

but this adds the title to the legend, rather than the main Clustermap.

I have also tried, creating an axes object first and adding the title to this (which seems to work for the Heatmap), but the problem is that the Clustermap does not plot on this axes object. It plots on its own object apparently.

Thanks for any help...

like image 596
agftrading Avatar asked Mar 13 '18 10:03

agftrading


People also ask

What is a Clustermap Seaborn?

The clustermap() function of seaborn plots a hierarchically-clustered heat map of the given matrix dataset. It returns a clustered grid index.

How do you save the plot in Seaborn?

To save a plot in Seaborn, we can use the savefig() method.


2 Answers

You can use .fig.suptitle('Your Title').

Imagine this example from the Seaborn documentation for a clustermap:

import seaborn as sns
iris = sns.load_dataset("iris")
species = iris.pop("species")
g = sns.clustermap(iris)

You add a title with:

g = sns.clustermap(iris).fig.suptitle('Your Title') 

Or simply:

g.fig.suptitle('Your Title') 

However, this adds a title to the whole figure and not to single subplots. If you want to add a title to a subplot, see @Kiraly Sandor's solution.

enter image description here

g.fig.suptitle('Figure Title')
g.ax_heatmap.set_title('Subplot Title')
like image 119
NK_ Avatar answered Oct 13 '22 22:10

NK_


One of the solutions could be this: the object returned by the plot has an ax_heatmap method that has a set_title method. If you set it with a title your plot will have one:

import seaborn as sns
sns.set(color_codes=True)
iris = sns.load_dataset("iris")
species = iris.pop("species")
g = sns.clustermap(iris)
g.ax_heatmap.set_title('lalal')

However this approach adds a title above your heatmap and not the whole plot.

like image 44
Zsolt Diveki Avatar answered Oct 13 '22 22:10

Zsolt Diveki