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.
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.
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.
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()
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With