-
-
Save foolishflyfox/4ef60826206b0ecb84f94e307f80838f to your computer and use it in GitHub Desktop.
Plot multiple images with matplotlib in a single figure. Titles can be given optionally as second argument.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import matplotlib.pyplot as plt | |
import numpy as np | |
import PIL | |
def show_images(images, rows = 1, titles = None): | |
"""Display a list of images in a single figure with matplotlib. | |
Parameters | |
--------- | |
images: List of np.arrays compatible with plt.imshow. | |
rows (Default = 1): Number of rows in figure | |
titles: List of titles corresponding to each image. Must have | |
the same length as titles. | |
""" | |
assert((titles is None)or (len(images) == len(titles))) | |
# both Image type and numpy.array element can be display | |
if isinstance(images[0], PIL.Image.Image): | |
images = [np.asarray(img) for img in images] | |
n_images = len(images) | |
if titles is None: titles = ['Image (%d)' % i for i in range(1,n_images + 1)] | |
fig = plt.figure() | |
for n, (image, title) in enumerate(zip(images, titles)): | |
a = fig.add_subplot(rows, np.ceil(n_images/float(rows)), n + 1) | |
if image.ndim == 2: | |
plt.gray() | |
plt.imshow(image) | |
a.set_title(title) | |
plt.xticks([]) | |
plt.yticks([]) | |
fig.set_size_inches(np.array(fig.get_size_inches()) * n_images) | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment