Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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()
@Sarnath

This comment has been minimized.

Copy link

@Sarnath Sarnath commented Nov 11, 2017

Super cool! Thanks buddy!

@durnezj

This comment has been minimized.

Copy link

@durnezj durnezj commented Dec 15, 2017

Exactly what I needed, thank you!

@MSC19950601

This comment has been minimized.

Copy link

@MSC19950601 MSC19950601 commented Mar 14, 2018

Super cool! Thanks buddy!

@PCJohn

This comment has been minimized.

Copy link

@PCJohn PCJohn commented Jun 1, 2018

This is really useful! Thanks!

@Bovey0809

This comment has been minimized.

Copy link

@Bovey0809 Bovey0809 commented Jun 10, 2018

thanks!

@shihgianlee

This comment has been minimized.

Copy link

@shihgianlee shihgianlee commented Jul 30, 2018

Thank you for this. Works out of the box!

@shang-vikas

This comment has been minimized.

Copy link

@shang-vikas shang-vikas commented Aug 29, 2018

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

This comment has been minimized.

Copy link

@abhisheksambyal abhisheksambyal commented Oct 11, 2018

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

@kamathy

This comment has been minimized.

Copy link

@kamathy kamathy commented Nov 6, 2018

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

AssertionError

@Jojozzc

This comment has been minimized.

Copy link

@Jojozzc Jojozzc commented Nov 30, 2018

smart tool

@irvingliao

This comment has been minimized.

Copy link

@irvingliao irvingliao commented Dec 10, 2018

Cool!

@mohammadjabbari7594

This comment has been minimized.

Copy link

@mohammadjabbari7594 mohammadjabbari7594 commented Jan 7, 2019

Thanks a lot

@vivekmaru13

This comment has been minimized.

Copy link

@vivekmaru13 vivekmaru13 commented Feb 14, 2019

Great utility. Thanks man!

@hasibzunair

This comment has been minimized.

Copy link

@hasibzunair hasibzunair commented Mar 4, 2019

Good stuff, thanks!

@nico00

This comment has been minimized.

Copy link

@nico00 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

This comment has been minimized.

Copy link

@AshishJSoman AshishJSoman commented Mar 31, 2019

Thanks sir !!

@sabritarik

This comment has been minimized.

Copy link

@sabritarik sabritarik commented May 27, 2019

Good job, thanks

@JohnTian

This comment has been minimized.

Copy link

@JohnTian JohnTian commented Jul 11, 2019

Thanks.

@amitadate

This comment has been minimized.

Copy link

@amitadate amitadate commented Jul 16, 2019

Thanks for sharing, good stuff!

@Alex-Kopylov

This comment has been minimized.

Copy link

@Alex-Kopylov Alex-Kopylov commented Jul 29, 2019

@soply Thank you dude

@dua8

This comment has been minimized.

Copy link

@dua8 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

This comment has been minimized.

Copy link

@Mehreenghaafoor Mehreenghaafoor commented Nov 7, 2019

when i run this code nothing to show any output

@sh4nr0ck

This comment has been minimized.

Copy link

@sh4nr0ck sh4nr0ck commented Mar 12, 2020

Thanks a lot!

@vaibhavjindal

This comment has been minimized.

Copy link

@vaibhavjindal vaibhavjindal commented Mar 29, 2020

Thanks, this really helped a lot!

@jacobeturpin

This comment has been minimized.

Copy link

@jacobeturpin jacobeturpin commented Apr 6, 2020

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

@15a15a

This comment has been minimized.

Copy link

@15a15a 15a15a commented Jun 1, 2020

Thanks a lot!

@h5ng

This comment has been minimized.

Copy link

@h5ng h5ng commented Jun 3, 2020

Thanks!

@Ehsan1997

This comment has been minimized.

Copy link

@Ehsan1997 Ehsan1997 commented Jun 29, 2020

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

@meir412

This comment has been minimized.

Copy link

@meir412 meir412 commented Aug 19, 2020

Thanks! matplotlib should implement this :)

@JunyiZhu-AI

This comment has been minimized.

Copy link

@JunyiZhu-AI JunyiZhu-AI commented Aug 28, 2020

Great!

@tyler8812

This comment has been minimized.

Copy link

@tyler8812 tyler8812 commented Oct 31, 2020

Great work!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.