I need to remove the empty spaces between some subplots, by expanding the subplots horizontally.
Here's an image to show what I mean with two nice freehand red circles (MWE below):
By "get rid" I mean: expand horizontally the two bottom subplots in each column, so that there's little to no white space between them. The white space that separates the columns must remain untouched, to avoid modifying the square subplots above.
I've tried fig.subplots_adjust(wspace=0)
but that messes up the above subplots, and I only need to modify the empty spaces between the bottom subplots.
I've also tried updating the GridSpec as shown here, but that doesn't get along with the tight_layout()
.
MWE
The MWE looks long but it's just the same code copy/pasted and with minor changes made (ie: removing ticks, labels, colorbar, etc).
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
from mpl_toolkits.axes_grid1 import make_axes_locatable
fig = plt.figure(figsize=(14.25, 13.5))
gs = gridspec.GridSpec(3, 6)
# Left top plot.
ax1 = plt.subplot(gs[0:1, 0:2])
plt.title('AAAAAAAA', fontsize=11)
ax1.tick_params(axis='both', which='major', labelsize=9)
ax1.set_xticklabels([])
plt.xlabel('', fontsize=12)
plt.ylabel('BBBB', fontsize=12)
SC = plt.scatter([0., 1.], [0., 1.], c=[2., 5.])
# Position colorbar.
the_divider = make_axes_locatable(ax1)
color_axis = the_divider.append_axes("right", size="2%", pad=0.1)
# Colorbar.
cbar = plt.colorbar(SC, cax=color_axis)
cbar.set_label('ccc', fontsize=12, labelpad=4, y=0.5)
cbar.ax.tick_params(labelsize=9)
ax1.set_aspect(aspect='auto')
# Left middle plot.
ax2 = plt.subplot(gs[1:2, 0:2])
ax2.tick_params(axis='both', which='major', labelsize=9)
plt.xlabel('DDDD', fontsize=12)
plt.ylabel('BBBB', fontsize=12)
SC = plt.scatter([10., 5.], [3., 17.], c=[0.3, 0.8])
the_divider = make_axes_locatable(ax2)
color_axis = the_divider.append_axes("right", size="2%", pad=0.1)
cbar = plt.colorbar(SC, cax=color_axis)
cbar.set_label('ccc', fontsize=12, labelpad=4, y=0.5)
cbar.ax.tick_params(labelsize=9)
ax2.set_aspect(aspect='auto')
# Left bottom left plot.
ax3 = plt.subplot(gs[2:3, 0:1])
ax3.tick_params(axis='both', which='major', labelsize=9)
plt.xlabel('DDDD', fontsize=12)
plt.ylabel('BBBB', fontsize=12)
# Left bottom right pot.
ax4 = plt.subplot(gs[2:3, 1:2])
ax4.tick_params(axis='both', which='major', labelsize=9)
ax4.set_yticklabels([])
plt.xlabel('DDDD', fontsize=12)
plt.ylabel('', fontsize=12)
# Middle top plot.
ax5 = plt.subplot(gs[0:1, 2:4])
plt.title('AAAAAAAA', fontsize=11)
ax5.tick_params(axis='both', which='major', labelsize=9)
ax5.set_xticklabels([])
ax5.set_yticklabels([])
plt.xlabel('', fontsize=12)
plt.ylabel('', fontsize=12)
SC = plt.scatter([0., 1.], [0., 1.], c=[2., 5.])
the_divider = make_axes_locatable(ax5)
color_axis = the_divider.append_axes("right", size="2%", pad=0.1)
cbar = plt.colorbar(SC, cax=color_axis)
cbar.set_label('ccc', fontsize=12, labelpad=4, y=0.5)
cbar.ax.tick_params(labelsize=9)
ax5.set_aspect(aspect='auto')
# Middle middle plot.
ax6 = plt.subplot(gs[1:2, 2:4])
ax6.tick_params(axis='both', which='major', labelsize=9)
ax5.set_yticklabels([])
plt.xlabel('DDDD', fontsize=12)
plt.ylabel('', fontsize=12)
SC = plt.scatter([10., 5.], [3., 17.], c=[0.3, 0.8])
the_divider = make_axes_locatable(ax6)
color_axis = the_divider.append_axes("right", size="2%", pad=0.1)
cbar = plt.colorbar(SC, cax=color_axis)
cbar.set_label('ccc', fontsize=12, labelpad=4, y=0.5)
cbar.ax.tick_params(labelsize=9)
ax6.set_aspect(aspect='auto')
# Middle bottom left plot.
ax7 = plt.subplot(gs[2:3, 2:3])
ax7.tick_params(axis='both', which='major', labelsize=9)
ax7.set_yticklabels([])
plt.xlabel('DDDD', fontsize=12)
plt.ylabel('', fontsize=12)
# Middle bottom right pot.
ax8 = plt.subplot(gs[2:3, 3:4])
ax8.tick_params(axis='both', which='major', labelsize=9)
ax8.set_yticklabels([])
plt.xlabel('DDDD', fontsize=12)
plt.ylabel('', fontsize=12)
SC = plt.scatter([10., 5.], [3., 17.], c=[0.3, 0.8])
the_divider = make_axes_locatable(ax8)
color_axis = the_divider.append_axes("right", size="5%", pad=0.1)
cbar = plt.colorbar(SC, cax=color_axis)
cbar.set_label('eee', fontsize=12, labelpad=4, y=0.5)
cbar.ax.tick_params(labelsize=9)
fig.tight_layout()
plt.savefig('del.png', dpi=150)