4

I'm making a figure with a total of 68 subplots and want to remove the empty space between them all. Here's what I have:

plot with too much space.

How would I go about doing this?

EDIT: using plt.tight_layout() makes it even worse:

even more space between the subplots

Here's my code:

for j in range(0,len(sort_yf)):
for i in range(0,len(yf)):
    if yf[i]==sort_yf[j]:
        sort_ID=np.append(sort_ID,'output/'+ID[i]+'.png')
for i in range (1,69):
    plt.subplot(17,4,i,aspect='equal')
    plots=img.imread(sort_ID[i])
    plt.imshow(plots)
    plt.axis('off')
plt.show()
zormit
  • 533
  • 4
  • 16
blablabla
  • 304
  • 1
  • 8
  • 18
  • I guess the answer to this question is given [here](https://stackoverflow.com/questions/41071947/how-to-remove-the-space-between-subplots-in-matplotlib-pyplot) as none of the answers here are really useful. – ImportanceOfBeingErnest Oct 02 '17 at 00:06

2 Answers2

6

Have you tried the tight layout functionality?

plt.tight_layout()

See also HERE

EDIT: Alternatively, you can use gridspec:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
images = [np.random.rand(40, 40) for x in range(68)]
gs = mpl.gridspec.GridSpec(17, 4)
gs.update(wspace=0.1, hspace=0.1, left=0.1, right=0.4, bottom=0.1, top=0.9) 
for i in range(68):
    plt.subplot(gs[i])
    plt.imshow(images[i])
    plt.axis('off')
plt.show()

enter image description here

SmCaterpillar
  • 6,683
  • 7
  • 42
  • 70
  • Hi, just tried this and unfortunately it didn't work. For some reason it seemed to make the problem worse. – blablabla Feb 09 '15 at 11:38
  • How did it get worse? Moreover, can you modify your example code snippet such that in can be executed? For instance, you did not specify `sort_yf`. – SmCaterpillar Feb 09 '15 at 11:46
  • each of the images actually got smaller, i'll upload an image of what i mean. I cant really because it requires a csv file and a folder full of images which are reasonably large. – blablabla Feb 09 '15 at 11:48
  • I guess gridspec may work in your case, see my editing of the post – SmCaterpillar Feb 09 '15 at 12:50
4

Adjust the whitespace around the subplots with:

plt.subplots_adjust(wspace=0.01,hspace=0.01)

In my example I just modified wspace and hspace. You can also adapt the positions. Consider the documentation.

You could also modify the GridSpec Layout.

zormit
  • 533
  • 4
  • 16