Display inline images in a Jupyter notebook with Matplotlib

2 minute read

Today I was working with the MNIST handwritten digits data and wanted to display a few images in a Jupyter notebook. After looking at PIL, then Pillow, I found the easiest way is to just use Matplotlib.

Here’s a code snippet that let’s you do it.

from matplotlib.pyplot import imshow
%matplotlib inline

w, h = 20, 20
image = X[0].reshape(w,h).T #assuming X[0] is of shape (400,)

imshow(image, cmap='gray')

MNIST handwritten digit

Note: You may still need Pillow when working with Matplotlib. See explanation and requirements here: Image tutorial.

Leave a Comment