10

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.

Jonas
  • 133
  • 1
  • 2
  • 7
  • Highly relevant : [Matplotlib 2 Subplots, 1 Colorbar](https://stackoverflow.com/questions/13784201/matplotlib-2-subplots-1-colorbar) – ImportanceOfBeingErnest Jun 06 '19 at 13:14
  • @ImportanceOfBeingErnest: Thank you for the reference. However, I am not sure how to add a colormap in this case. If I uncomment the lines in my code, I get: AttributeError: 'AxesSubplot' object has no attribute 'autoscale_None' – Jonas Jun 06 '19 at 13:32

2 Answers2

16

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

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
  • Thanks for the answer. Little question: why you specify vim only for the first heatmap and vmax only for the second? (I tried specifying both values for both heatmaps and it didn't work well) – Meni Apr 18 '23 at 08:40
7

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.

Paul H
  • 65,268
  • 20
  • 159
  • 136