2

I am trying to plot multiple sublpots using the following code

import matplotlib.pyplot as plt
from matplotlib import image

fig, ax = plt.subplots(nrows=10, ncols=3,squeeze= False, figsize = (40, 40))
i = 0
for row in ax:
    for col in row:
        img = image.imread("black.png")
        i = i+1
        col.axis("off")
        col.set_title("group_", fontsize = 20) 
        col.imshow(img) # consider I,m plottinf some image

When I'm plotting fewer subplots the subplots are very close together enter image description here

However When I'm plotting more subplots there is a considerable gap between horizontal subplots.

enter image description here

Let me know how can I plot such that the plots are close to each other

Note: This may look like duplicate but I was not able to find a solution by browsing stackoverflow to fix this issue

MNA
  • 183
  • 2
  • 8
  • 1
    have you tried [`plt.tight_layout()`](https://matplotlib.org/users/tight_layout_guide.html) – Sharku Oct 09 '18 at 13:37
  • 1
    Some questions: [One](https://stackoverflow.com/questions/49376052/preventing-matplotlib-from-decrease-the-size-of-plotted-images-as-number-of-imag), [Two](https://stackoverflow.com/questions/42475508/how-to-combine-gridspec-with-plt-subplots-to-eliminate-space-between-rows-of-s), [Three](https://stackoverflow.com/questions/41071947/how-to-remove-the-space-between-subplots-in-matplotlib-pyplot). – ImportanceOfBeingErnest Oct 09 '18 at 13:49

2 Answers2

1

What you are observing is an unfortunate combination of figsize parameter of plt.subplots() and imshow(aspect="") in conjunction with your image size. In particular, all those are constraining the vertical/horizontal spaces.

In my case, going from figsize=(40, 40) to figsize=(8, 24) somewhat fixed the issue.

figsize=(40, 40)

so_figsize_error

figsize=(8, 24)

so_figsize_fixed

norok2
  • 25,683
  • 4
  • 73
  • 99
0

You can adjust the horizontal and vertical space between subplots with hspace and wspace.

matplotlib.pyplot.subplots_adjust(hspace = ..., wspace = ...)
Dinesh
  • 1,555
  • 1
  • 16
  • 18
  • I tried it but still it didn't solved my problem. hspace is changing and wspace is increasing but not decreasing then what I already have. – MNA Oct 09 '18 at 14:18
  • @MNA, did you also try negative values? it supports negative values and e.g. wspace=-0.6 would decrease space compared to default. – Dinesh Oct 09 '18 at 14:27