Skip to content

Instantly share code, notes, and snippets.

@soply
Last active January 9, 2024 14:52
Show Gist options
  • Star 96 You must be signed in to star a gist
  • Fork 17 You must be signed in to fork a gist
  • Save soply/f3eec2e79c165e39c9d540e916142ae1 to your computer and use it in GitHub Desktop.
Save soply/f3eec2e79c165e39c9d540e916142ae1 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.
import matplotlib.pyplot as plt
import numpy as np
def show_images(images, cols = 1, titles = None):
"""Display a list of images in a single figure with matplotlib.
Parameters
---------
images: List of np.arrays compatible with plt.imshow.
cols (Default = 1): Number of columns in figure (number of rows is
set to np.ceil(n_images/float(cols))).
titles: List of titles corresponding to each image. Must have
the same length as titles.
"""
assert((titles is None)or (len(images) == len(titles)))
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(cols, np.ceil(n_images/float(cols)), n + 1)
if image.ndim == 2:
plt.gray()
plt.imshow(image)
a.set_title(title)
fig.set_size_inches(np.array(fig.get_size_inches()) * n_images)
plt.show()
@shihgianlee
Copy link

Thank you for this. Works out of the box!

@shang-vikas
Copy link

worked like a charm. Added an additional parameter to control the scaling.
line 27 : instead of n_images , i used n_images/scale. scale of 4 works fine. users can tweak

@abhisheksambyal
Copy link

@soply Thanks!
@shang-vikas scaling is good! Thanks!

@kamathy
Copy link

kamathy commented Nov 6, 2018

assert((titles is None)or (len(images) == len(titles)))

AssertionError

@Jojozzc
Copy link

Jojozzc commented Nov 30, 2018

smart tool

@irvingliao
Copy link

Cool!

@mohammadjabbari7594
Copy link

Thanks a lot

@vivekmaru13
Copy link

Great utility. Thanks man!

@hasibzunair
Copy link

Good stuff, thanks!

@nico00
Copy link

nico00 commented Mar 19, 2019

Good job, but please note that columns and rows are swapped in add_subplot.
The correct usage should be:
a = fig.add_subplot(np.ceil(n_images/float(cols)), cols, n + 1)

@AshishJSoman
Copy link

Thanks sir !!

@sabritarik
Copy link

Good job, thanks

@Digital2Slave
Copy link

Thanks.

@amitadate
Copy link

Thanks for sharing, good stuff!

@Alex-Kopylov
Copy link

@soply Thank you dude

@dua8
Copy link

dua8 commented Sep 15, 2019

this code doesn't give any error but also it doesn't show any output. so what i do?

@Mehreenghaafoor
Copy link

when i run this code nothing to show any output

@vinacioo
Copy link

Thanks a lot!

@vaibhavjindal
Copy link

Thanks, this really helped a lot!

@jacobeturpin
Copy link

Was trying to figure out how to do this elegantly in a function. Thanks a lot for posting this!

@15a15a
Copy link

15a15a commented Jun 1, 2020

Thanks a lot!

@h5ng
Copy link

h5ng commented Jun 3, 2020

Thanks!

@Ehsan1997
Copy link

Was looking to implement this myself, you saved me a lot of minutes. Thank you very much!

@meir412
Copy link

meir412 commented Aug 19, 2020

Thanks! matplotlib should implement this :)

@JunyiZhu-AI
Copy link

Great!

@tyler8812
Copy link

Great work!!

@nathlemma
Copy link

Thank you so much!

@AminShahidi98
Copy link

It was useful, thank you!

@atgctg
Copy link

atgctg commented May 24, 2022

If you're getting the following error like me:

ValueError: Number of rows must be a positive integer

You can fix it by casting the row number to int:

a = fig.add_subplot(int(np.ceil(n_images / float(cols))), cols, n + 1)

Also swapped the args order, number of rows should come first.

@lhfy127
Copy link

lhfy127 commented Dec 9, 2023

How to save the multiple-image figure as a new image?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment