0

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.

Zephyr
  • 11,891
  • 53
  • 45
  • 80
John M
  • 177
  • 3
  • 14
  • Please provide an [mcve] we can play around with – Joooeey May 29 '22 at 12:15
  • @Joooeey did you see what I have written? If iam able to provide a reproducible example the problem would be solved. Essentially I want to know how you can combine the subplots into a single plot. – John M May 29 '22 at 12:19
  • 1
    you can provide a reproducable example that produces a plot similar to the one in your image. – Joooeey May 29 '22 at 12:20
  • 1
    Does this answer your question? [How to remove the space between subplots in matplotlib.pyplot?](https://stackoverflow.com/questions/41071947/how-to-remove-the-space-between-subplots-in-matplotlib-pyplot) – Joooeey May 29 '22 at 12:40

2 Answers2

1

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.

Joooeey
  • 3,394
  • 1
  • 35
  • 49
1

You have to specify the grid parameters:

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

with matplotlib.pyplot.subplots:

fig, axes = plt.subplots(nrows = 2, ncols = 3, gridspec_kw = {'wspace': 0, 'hspace': 0})

Then you can loop over created axes and, for each one of them, you have to show the image and set axis to 'tight' firtsly and 'off' secondly:

for ax in axes.flatten():
    ax.imshow(img)
    ax.axis('tight')
    ax.axis('off')

Your code would be slighlty different, since you are plotting different images for each ax.

Complete Code

import matplotlib.pyplot as plt

img = plt.imread('img.jpeg')

fig, axes = plt.subplots(nrows = 2, ncols = 3, gridspec_kw = {'wspace': 0, 'hspace': 0})

for ax in axes.flatten():
    ax.imshow(img)
    ax.axis('tight')
    ax.axis('off')

plt.show()

enter image description here

Zephyr
  • 11,891
  • 53
  • 45
  • 80
  • setting to "tight" will change the aspect ratio. – Joooeey May 29 '22 at 12:53
  • 1
    @Zephyr: this is awesome. Joey it does not seem to change the aspect ratio: but perhaps my image is square so I dont see it? – John M May 29 '22 at 12:56
  • Did you measure it? Did it stay exactly square? From your screenshot it looks like the plot spots in the 2x3 grid are almost square but not exactly. – Joooeey May 29 '22 at 13:23
  • @Joooeey well all my 6 images are exactly 128x128 - so I presume there cannot be any change in a/r? or am i not understanding you? :( – John M May 29 '22 at 15:50
  • @JohnM this can only work by happenstance. For a general figure size or sized image there will be white space using this approach. Try changing the size of your figure. – Jody Klymak May 30 '22 at 06:47
  • @JodyKlymak no, if you call `ax.axis('tight')`, the image will be stretched, no white space. Unless as you say, the individual plots end up square by chance. How to solve that problem is well explained in answers to the duplicate question that I linked. – Joooeey May 30 '22 at 12:47
  • @Joooeey. Yes, that will happen if you are OK with your image being arbitrarily stretched. Most people are not. – Jody Klymak May 31 '22 at 05:08