0

I plot an array of images as subplots as follows:

fig, ax = plt.subplots(nrows=32,ncols=3, sharex=True, sharey=True)
...
for n in range(32*3):
  ax = plt.subplot(32, 3, n)      # select where to plot
  plt.imshow(filt, cmap='gray')   # plot image
  n += 1
...
plt.show()

but the images are too small - seems like the plt.show() holds the horizontal/vertical ratio to 1, so the overall plot is square, but should be 32/3 ratio. As a result, my images look very small and distant within columns: enter image description here I tried using tight_layout() as suggested here and tried plt.axes().set_aspect(32/3) but it did not have any good effect. There are many examples that use equal number of subplots horizontally and vertically, so the subplots look normal, but in my case I have 32 rows and 3 columns which is scaled weirdly. What am I missing?

Nazar
  • 820
  • 5
  • 13
  • 36
  • Tried using `plt.subplots_adjust (wspace=0.1)`? – Ashish Nov 09 '19 at 16:41
  • @Ashish that did not help. Ideally, I would not want to specify this spacing. I expect the function to automatically size each subplot. As I mentioned earlier, if my subplot is of equal sides, for example (4x4), then subplots are evenly spaced with minimal distance of each other. But my case (32,3) does not work. – Nazar Nov 09 '19 at 16:53
  • See [this](https://stackoverflow.com/questions/42475508/how-to-combine-gridspec-with-plt-subplots-to-eliminate-space-between-rows-of-s) or [this](https://stackoverflow.com/questions/49376052/preventing-matplotlib-from-decrease-the-size-of-plotted-images-as-number-of-imag/49386898#49386898). – ImportanceOfBeingErnest Nov 09 '19 at 17:45
  • @ImportanceOfBeingErnest the second link seems to be the same problem as mine. Though, I can not figure out how to adapt the solution to my _forloop_. I included this line `fig, ax = plt.subplots(nrows=32,ncols=3, sharex=True, sharey=True)` before the _forloop_ (see updated code), but the final plot is the same. – Nazar Nov 09 '19 at 18:07
  • The second link uses the same formulas as shown in the first. Both codes use for loops. – ImportanceOfBeingErnest Nov 09 '19 at 19:09
  • What is `filt.shape`?  Matplotlib tries very hard to draw square pixels... In other terms, adjust the `figsize` as suggested elsewhere. – gboffi Nov 09 '19 at 22:28
  • @gboffi the pixels are drawn as squares. As you can see, each image is a made of 3x3 pixels and is square, so the pixels must be squares too. – Nazar Nov 09 '19 at 23:23
  • If you do not change the `figsize`, as beautifully explained in the answers that ImportanceOfBeingErnest had kindly linked, because your `imshow`s are square and Matplotlib **forces** the aspect ratio of pixels to square, then the result cannot be different from what you've got. REPEAT: change the `figsize`. – gboffi Nov 09 '19 at 23:26
  • @gboffi thank you - adjusting `figsize` helped a little - my figure is now rectangular. However, the horizontal spacing between columns is still large and adjusting `wspace` and `hspace` as in the following `fig, axes = plt.subplots(nrows=32, ncols=3, figsize=(3, 32))` and `plt.subplots_adjust(wspace=0.01, hspace=0.1)` does not help. Any suggestions? – Nazar Nov 10 '19 at 01:30

0 Answers0