0

I need to plot a 2x2 array of heatmaps using seaborn and with a fixed figsize. As the bar for the heatmaps on the right is the same, I decided to take out the upper one, and the same for the heatmaps on the right.

Original array:

However, 1) the upper heatmaps change their aspect to compensate for the space from the bar. Also, 2) the bar overlaps the heatmap and I cannot make the plot bigger, so it has to fix within the figsize.

Modified array:

My code:

df1 = pd.DataFrame(np.random.randint(0,100,size=(100, 4)), columns=list('ABCD'))
df2 = df1*1000

df3 = pd.DataFrame(np.random.randint(0,100,size=(100, 4)), columns=list('ABCD'))
df4 = df3*1000

# Plot features
sns.set_theme()
sns.set_context("paper")
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2,2,sharey=True,sharex=True,figsize=(7.48,7.48),frameon=True) # Width - Height
plt.rcParams['font.size'] = 6
plt.rcParams['axes.linewidth'] = 2

#Heatmaps
sns.heatmap(df1,ax=ax1,cbar_kws=dict(label= 'adim.',location='bottom',use_gridspec=False),vmax=100)
sns.heatmap(df2,ax=ax2,cbar_kws=dict(label= 'adim.',location='bottom',use_gridspec=False),vmax=100000)
sns.heatmap(df3,ax=ax3,cbar_kws=dict(label= 'adim.',location='bottom',use_gridspec=False),vmax=100)
sns.heatmap(df4,ax=ax4,cbar_kws=dict(label= 'adim.',location='bottom',use_gridspec=False),vmax=100000)

  #Plot detail
ax1.set_xlabel('Year')
ax2.set_xlabel('Year')
ax3.set_xlabel('Year')
ax4.set_xlabel('Year')

ax1.set_ylabel('mm-dd')
ax2.set_ylabel('mm-dd')
ax3.set_ylabel('mm-dd')
ax4.set_ylabel('mm-dd')

  #Save Plot
fig = plt.gcf()
plt.savefig(f"{images_dir}/Stack1.png",dpi=100)
plt.show()
JohanC
  • 59,187
  • 8
  • 19
  • 45
  • See e.g. [How to have one colorbar for all subplots](https://stackoverflow.com/questions/13784201/how-to-have-one-colorbar-for-all-subplots) – JohanC Dec 09 '21 at 14:08

0 Answers0