Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create multiple seaborn heatmaps with a shared legend in one figure?

Tags:

python

seaborn

I want to create multiple (here: two) seaborn heatmaps in one figure where the heatmaps have different value ranges but should contain a single, shared legend.

The following code creates two heatmaps in a single figure, but the color-coding is different for the two plots.

import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd

df = pd.DataFrame(np.random.random((4,4)))
df2 = pd.DataFrame([1., .5, .9, .6])

fig, ax = plt.subplots(ncols=2, sharey=True)

sns_g = sns.heatmap(df, annot=True, yticklabels=True, ax=ax[0])
sns_g2 = sns.heatmap(df2, annot=True, cbar=False, ax=ax[1])

# fig.subplots_adjust(right=0.6)
# cbar_ax = fig.add_axes([1.1, 0.15, 0.05, .77])
# fig.colorbar(ax[1], cax=cbar_ax)

plt.tight_layout()

plt.show()

I included cbar=True in sns_g only to show that the two legends represent a different range. I want explicitly create two plots.

like image 1000
Jonas Avatar asked Jun 06 '19 13:06

Jonas


People also ask

How do you combine heatmaps?

To concatenate heatmaps, simply use + operator. Under default mode, dendrograms from the second heatmap will be removed and row orders will be the same as the first one. Also row names for the first two heatmaps are removed as well. The returned value of the concatenation is a HeatmapList object.

How do you make multiple plots in Seaborn?

In Seaborn, we will plot multiple graphs in a single window in two ways. First with the help of Facetgrid() function and other by implicit with the help of matplotlib. data: Tidy dataframe where each column is a variable and each row is an observation.


Video Answer


2 Answers

If you want to create the colorbar manually, you can preserve an axes in the grid for it. Use vmin and vmax to specify the limits, and use one of the heatmaps ( e.g. axs[0].collections[0]) as ScalarMappable input to fig.colorbar.

import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd

df = pd.DataFrame(np.random.random((4,4)))
df2 = pd.DataFrame([1., .5, .9, .6])

vmin = min(df.values.min(), df2.values.min())
vmax = max(df.values.max(), df2.values.max())

fig, axs = plt.subplots(ncols=3, gridspec_kw=dict(width_ratios=[4,1,0.2]))

sns.heatmap(df, annot=True, cbar=False, ax=axs[0], vmin=vmin)
sns.heatmap(df2, annot=True, yticklabels=False, cbar=False, ax=axs[1], vmax=vmax)

fig.colorbar(axs[1].collections[0], cax=axs[2])

plt.show()

enter image description here

like image 181
ImportanceOfBeingErnest Avatar answered Nov 14 '22 21:11

ImportanceOfBeingErnest


fig.colorbar wants a ScalarMappable object, not an Axes. But seaborn's API let's you not worry about that.

According to its docs, which I found by searching for "heatmap" within seaborn.pydata.org, the function signature is:

seaborn.heatmap(data, vmin=None, vmax=None, cmap=None, center=None,
                robust=False, annot=None, fmt='.2g', annot_kws=None, 
                linewidths=0, linecolor='white', cbar=True, cbar_kws=None, 
                cbar_ax=None, square=False, xticklabels='auto',
                yticklabels='auto', mask=None, ax=None, **kwargs)

So all you need to do is pass the same vmin and vmax values to both heatmaps, but pass cbar=False, and cbar=True to the other.

like image 33
Paul H Avatar answered Nov 14 '22 22:11

Paul H