'Matplotlib - How to remove color bar but keep the heatmap position unchanged

I made a figure with 3 axes in it. Each axis is a heatmap with the same color bar. I want to only keep the colorbar of the 3rd axis and hide the 1st and 2nd colorbar (but keep the heatmap position unchanged). How could I do it?

Here is my code:

fig=plt.figure()
grid = plt.GridSpec(4, 6)

plt.subplot(grid[0:2,0:5])
ax1=sns.heatmap(df_norm, cmap='Reds', vmin=0.05, vmax=0.7)
ax1.axes.get_yaxis().set_visible(False)
ax1.xaxis.tick_top()
ax1.set_xticklabels(col, rotation=90)

plt.subplot(grid[2,0:5])
ax2=sns.heatmap(df_tgfup, cmap='Reds', vmin=0.05, vmax=0.7)
ax2.axes.get_xaxis().set_visible(False)
ax2.axes.get_yaxis().set_visible(False)
ax2.xaxis.tick_top()
ax2.set_xticklabels(col, rotation=90)

plt.subplot(grid[3,0:5])
ax3=sns.heatmap(df_tgfdown, cmap='Reds', vmin=0.05, vmax=0.7)
ax3.axes.get_xaxis().set_visible(False)
ax3.axes.get_yaxis().set_visible(False)
ax3.xaxis.tick_top()
ax3.set_xticklabels(col, rotation=90)

Here is the fig I made:
fig I made

And here is the fig I want to make:
fig I want to make



Solution 1:[1]

The colorbars are defined as axes in your figure. You can access them in fig.axes

[<AxesSubplot:>,
 <AxesSubplot:label='<colorbar>'>,
 <AxesSubplot:>,
 <AxesSubplot:label='<colorbar>'>,
 <AxesSubplot:>,
 <AxesSubplot:label='<colorbar>'>]

You can use the set_visible(False) method to hide them:

fig=plt.figure()
grid = plt.GridSpec(4, 6)

df_norm = pd.DataFrame(np.random.randint(0,10,size=(10, 10)), columns=list('ABCDEFGHIJ'))

plt.subplot(grid[0:2,0:5])
ax1=sns.heatmap(df_norm, cmap='Reds', vmin=0.05, vmax=0.7)
ax1.axes.get_yaxis().set_visible(False)
ax1.xaxis.tick_top()
#ax1.set_xticklabels(col, rotation=90)
fig.axes[1].set_visible(False)

plt.subplot(grid[2,0:5])
ax2=sns.heatmap(df_norm, cmap='Reds', vmin=0.05, vmax=0.7)
ax2.axes.get_xaxis().set_visible(False)
ax2.axes.get_yaxis().set_visible(False)
ax2.xaxis.tick_top()
#ax2.set_xticklabels(col, rotation=90)
fig.axes[3].set_visible(False)

plt.subplot(grid[3,0:5])
ax3=sns.heatmap(df_norm, cmap='Reds', vmin=0.05, vmax=0.7)
ax3.axes.get_xaxis().set_visible(False)
ax3.axes.get_yaxis().set_visible(False)
ax3.xaxis.tick_top()
#ax3.set_xticklabels(col, rotation=90)

multiple heatmaps with hidden colorbars

Solution 2:[2]

You can pass in cbar=False argument to sns.heatmap() wherever you wish to hide the color bar of the heatmap.

i.e.,

ax1=sns.heatmap(df_norm, cmap='Reds', cbar=False, vmin=0.05, vmax=0.7)
ax2=sns.heatmap(df_tgfup, cmap='Reds', cbar=False, vmin=0.05, vmax=0.7)

while ax3 will remain unchanged.

Then to keep the position of subplot 1 & 2 unchanged, you can play around with plt.subplots_adjust(right=num); where num is the position of the right edge of the subplots, as a fraction of the figure width which you'll decide for your desirable adjustment of the subplots.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 mozway
Solution 2