'Adding jpg pictures to a subplot- hspace and wspace different than specified

I am trying to create a single figure by adding several jpg figures. However, hspace and space between plots are always different than specified

import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import matplotlib as mpimg
import matplotlib.gridspec as gridspec
import matplotlib.pyplot as plt
import matplotlib.image as mpimg

## get savd pngs ##

img3 = mpimg.imread(r'C:\Users\nikos.000\coh_struc_dist_final\figures\jpg\PSD\_all_psd_20_100.jpg')
img2 = mpimg.imread(r'C:\Users\nikos.000\coh_struc_dist_final\figures\jpg\PSD\_all_psd_100_1000.jpg')
img1 = mpimg.imread(r'C:\Users\nikos.000\coh_struc_dist_final\figures\jpg\PSD\_all_psd_1000_5000.jpg')
img6 = mpimg.imread(r'C:\Users\nikos.000\coh_struc_dist_final\figures\jpg\PSD\_all_time_psd_20_100.jpg')
img5 = mpimg.imread(r'C:\Users\nikos.000\coh_struc_dist_final\figures\jpg\PSD\_all_time_psd_100_1000.jpg')
img4 = mpimg.imread(r'C:\Users\nikos.000\coh_struc_dist_final\figures\jpg\PSD\_all_time_psd_1000_5000.jpg')

size_f = 15
fsize  = 7
labels = ("(a)", "(b)", "(c)", "(d)","(e)", "(f)" )
fig = plt.figure()
gs = gridspec.GridSpec(2, 3,wspace=0.01,hspace=0.01)

ax1 = fig.add_subplot(gs[0,0], )
ax2 = fig.add_subplot(gs[0, 1])
ax3 = fig.add_subplot(gs[0, 2], )
ax4 = fig.add_subplot(gs[1,0])
ax5 = fig.add_subplot(gs[1,1], )
ax6 = fig.add_subplot(gs[1, 2])
#ax3 = fig.add_subplot(gs[1, 1:3])
ax1.imshow(img1)
ax1.text(-0.00015, 0.95, labels[0], transform=ax1.transAxes, fontsize=fsize, fontweight='normal', va='top', ha='right')
ax2.imshow(img2)
ax2.text(-0.00015, 0.95, labels[1], transform=ax2.transAxes, fontsize=fsize, fontweight='normal', va='top', ha='right')
ax3.imshow(img3)
ax3.text(-0.00015, 0.95, labels[2], transform=ax3.transAxes, fontsize=fsize, fontweight='normal', va='top', ha='right')
ax4.imshow(img4)
ax4.text(-0.015, 0.95, labels[3], transform=ax4.transAxes, fontsize=fsize, fontweight='normal', va='top', ha='right')

ax5.imshow(img5)
ax5.text(-0.015, 0.95, labels[4], transform=ax5.transAxes, fontsize=fsize, fontweight='normal', va='top', ha='right')

ax6.imshow(img6)
ax6.text(-0.015, 0.95, labels[5], transform=ax6.transAxes, fontsize=fsize, fontweight='normal', va='top', ha='right')

ax1.axis('off')
ax2.axis('off')

ax3.axis('off')
ax4.axis('off')

ax5.axis('off')
ax6.axis('off')
 
gs.tight_layout(fig)
fig.tight_layout()
fig.subplots_adjust(bottom=0.5)


fig.savefig(r'C:\Users\nikos.000\coh_struc_dist_final\figures\jpg\PSD\all.jpg', format='jpg',dpi=300,bbox_inches='tight')
#fig.savefig(r'C:\Users\nikos.000\vlahos\eps\fig14.eps', format='eps',dpi=300)#,bbox_inches='tight')

This is what I am getting as a result:

enter image description here

Is there a way I could reduce the space between the plots?



Sources

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

Source: Stack Overflow

Solution Source