简体   繁体   中英

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

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.

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 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 ( eg 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()

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM