Home > Net >  matplotlib combining subplots into a single plot with no axis and no gaps
matplotlib combining subplots into a single plot with no axis and no gaps

Time:05-30

I have an image which looks like so:

enter image description here

It was generated using matplotlib using:

for slice_idx in range(mandrill_t.highpasses[1].shape[2]):
    print(slice_idx)
    subplot(2, 3, slice_idx 1)
    imshow(np.abs(mandrill_t.highpasses[1][:,:,slice_idx]), cmap='Spectral', clim=(0, 1))

However, for my use case, I would like all these 6 images in a single image with no gaps or axis - I do not have an example output image to show, but essentially, I would like them stacked horizontally (3 of them) and vertically (2 of them) so that the 6 images are a single image.

I tried looking around for similar problems to draw inspiration from, but no luck so far :(

Any pointers would be great.

CodePudding user response:

You have to specify the grid parameters:

  • 2 rows
  • 3 columns
  • 0 width space
  • 0 height space

with enter image description here

CodePudding user response:

That's what GridSpec is for (see plt.subplots docs):

Just add the following line at the start:

subplots(2, 3, gridspec_kw={"wspace": 0, "hspace": 0})

You might also have to set some plot elements to invisible but it's hard to figure out exactly which without an MCVE.

  • Related