matplotlib display images from left to right to bottom (matlab montage)

3.1k Views Asked by At

Is there a way I can display a list of images with one function ? In matlab there is montage. Currently if I for loop and imshow matplotlib displays the images from top to bottom. I would like it to be from left to right then to bottom when no space. My code is below but it is stupid code of course.

import numpy as np

plot_image = image_list[1]
for i in range(20):
    plot_image = np.concatenate((plot_image, image_list[i+1]), axis=1)

plt.figure(figsize = (15,15))
plt.imshow(plot_image, cmap = 'gray');

So is there like a function montage(list_of_images) or even better a function that does not need to take in images of the same size like smarter_imshow(list_of_images_with_unequal_resolution)

2

There are 2 best solutions below

0
TheBlackCat On

You can do something like this with the ImageGrid class.

This is adapted from the above documentation:

import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import ImageGrid
import numpy as np

fig = plt.figure(1)
grid = ImageGrid(fig, 111,  # similar to subplot(111)
                 nrows_ncols=(2, 2),  # creates 2x2 grid of axes
                 axes_pad=0,  # pad between axes in inch.
                 )

for i in range(4):
    grid[i].imshow(np.random.random((10, 10)))  # The AxesGrid object work as a list of axes.
    grid[i].axis('off')
    grid[i].set_xticks([])
    grid[i].set_yticks([])
0
black_puppydog On

The upcoming version of scikit-image has an implementation of montage that works for color images:

https://github.com/scikit-image/scikit-image/blob/master/skimage/util/_montage.py

You do have to pass multichannel=True for color images though.

Awaiting the release of that version, that file is pretty easy to modify (just fix the imports at the top) so it works standalone.