Remove spacing between images in matplotlib

Viewed 423

I want to display MNIST images as a composite image using matplotlib. Right now it is displaying the individual images on a horizontal plane (1 row, x columns) but there is whitespace between each image.

How can I bring the images right next to each other (removing the vertical whitespace between each MNIST image) so it looks like 1 single image?

Here is the relevant part of my code:

for x in range(5):
    digit = train_images[x]
    plt.box(False)
    plt.subplot(1, 5, x+1)
    plt.imshow(digit)
    plt.axis('off')
    plt.grid(b=None)
#plt.title('50419 -- my Name', loc='center')
plt.show()

This is what the current output looks like:

current output

2 Answers

Actually, I was able to fix it with just a single line of code:

plt.tight_layout(pad=0.00)

This brought the images right next to each other.

Maybe you could try with this 'fig' method:

fig.subplots_adjust(wspace = 0)

First you would have to create a fig and an axis:

fig, ax = plt.subplots(1, 5)
Related