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.

Like to comment? Feel free to send me an email or reach out on Twitter.

Did this or another article help you? If you like and can afford it, you can buy me a coffee (3 EUR) ☕️ to support me in writing more posts. In case you would like to contribute more or I helped you directly via email or coding/troubleshooting session, you can opt to give a higher amount through the following links or adjust the quantity: 50 EUR, 100 EUR, 500 EUR. All links redirect to Stripe.