1

I have four 2D arrays I want to plot in four subplots using imshow. I want the separation between these subplots to be removed, making the subplots touch each other, similar to the Matplotlib Documentation (second to last example). My attempt is

fig, axs = plt.subplots(2, 2, sharex='col', sharey='row', gridspec_kw={'hspace': 0, 'wspace': 0})
(ax1, ax2), (ax3, ax4) = axs

ax1.imshow(im1)
ax2.imshow(im2)
ax3.imshow(im3)
ax4.imshow(im4)

for ax in fig.get_axes():
    ax.label_outer()

plt.show()

This produces

enter image description here

The separation in the vertical direction seems to be correctly removed, but I still have a separation in the horizontal direction. Does anyone know how I can get rid of that as well here?

The One
  • 83
  • 7

1 Answers1

1

You can try something along the lines of this answer of ImportanceOfBeingErnest. I have prepared the following pseudo code based on it for your question. You can try it and see if it works for you.

from matplotlib import gridspec

nrow, ncol = 2, 2

fig = plt.figure(figsize=(6,6)) 

gs = gridspec.GridSpec(nrow, ncol,
         wspace=0.0, hspace=0.0, 
         top=1.-0.5/(nrow+1), bottom=0.5/(nrow+1), 
         left=0.5/(ncol+1), right=1-0.5/(ncol+1)) 

ims = [im1, im2, im3, im4]

c = 0 # Counter for the ims array

for i in range(nrow):
    for j in range(ncol):
        ax= plt.subplot(gs[i,j])
        ax.imshow(ims[c])
        ax.set_xticklabels([])
        ax.set_yticklabels([])
        c += 1

for ax in fig.get_axes():
    ax.label_outer()       
Sheldore
  • 37,862
  • 7
  • 57
  • 71