03-03-2020, 03:30 PM
Matplotlib Imshow — A Helpful Illustrated Guide
I always struggled with the method of Python’s matplotliblibrary. To help you and me master it, I committed to write the most in-depth resource about it on the web.
As you study the resource, you can play my explainer video that leads you through the code in this article:
To show an image in matplotlib, first read it in using, then display it with .
![[Image: img0.png]](https://raw.githubusercontent.com/theadammurphy/matplotlib_articles/master/imshow/final_html/img/img0.png)
To turn the (annoying) axis ticks off, call.
![[Image: img1.png]](https://raw.githubusercontent.com/theadammurphy/matplotlib_articles/master/imshow/final_html/img/img1.png)
Much better! But there is a lot more you can do than just show images. Let’s look at how this works in more detail.
Matplotlib Imshow Example
When you display an in image in matplotlib, there are 2 steps you need to take: first you read the image and then you show it.
You read in the image using and pass it a string. I have the images stored in a directory called Figures, so I first write followed by the name of the image with its file extension – . If your images are stored in your current working directory, you can omit .
I store the output of in a variable with a descriptive name because you need to pass this to . So, the first line is .
Images are made up of pixels and each pixel is a dot of color. The cat image is 1200×800 pixels. When an image is loaded into a computer, it is saved as an array of numbers. Each pixel in a color image is made up of a Red, Green and Blue (RGB) part. It can take any value between 0 and 255 with 0 being the darkest and 255 being the brightest. In a grayscale image, each pixel is represented by just a single number between 0 and 1. If a pixel is 0, it is completely black, if it is 1 it is completely white. Everything in between is a shade of gray.
So, if the cat image was black and white, it would be a 2D numpy array with shape. As it is a color image, it is in fact a 3D numpy array (to represent the three different color channels) with shape .
Note that Numpy writes image sizes is the opposite way to matplotlib and the ‘real world’. Numpy is from the world of mathematics and matrices where you always write the number of rows (height) first followed by the columns (width). If you need a quick NumPy refresher, check out my in-depth NumPy tutorial on this blog.
Matplotlib bases its image functions on the ‘real world’ and so displays the width first followed by the height.
Let’s look at the type and size of.
Once you’ve read your image into a numpy array, it’s time to display it using. This is similar to which you call at the end of any matplotlib plot. However, unlike , you must pass the image you want to display as an argument. This is helpful if you have read in multiple images but only want to display a certain number of them.
![[Image: img2.png]](https://raw.githubusercontent.com/theadammurphy/matplotlib_articles/master/imshow/final_html/img/img2.png)
I’m not sure why but, by default, all images are shown with axis ticks and labels. This can be quite annoying, so call to remove them.
However, ticks and labels can be helpful if you only want to select part of the image. By using them as guides, I’ll slice to just get the head of our cute kitten.
![[Image: img3.png]](https://raw.githubusercontent.com/theadammurphy/matplotlib_articles/master/imshow/final_html/img/img3.png)
The above image is a jpeg but you can also display other types of image files such as pngs.
![[Image: img4.png]](https://raw.githubusercontent.com/theadammurphy/matplotlib_articles/master/imshow/final_html/img/img4.png)
You can display gifs but it is slightly more complicated to do so and so is outside the scope of this article. If you read in and show a gif using the above steps, matplotlib will just show one of its frames.
![[Image: img5.png]](https://raw.githubusercontent.com/theadammurphy/matplotlib_articles/master/imshow/final_html/img/img5.png)
Matplotlib Imshow Grayscale
You can turn any color image into a grayscale image in matplotlib. Since grayscale images are 2D numpy arrays, use slicing to turn your 3D array into a 2D one.
If you write or you will get different arrays but the final image will still look the same. This is because grayscale images just care about the relative difference of intensity between each pixel and this is the same over each RGB channel.
Let’s plot it and see what happens.
![[Image: img6.png]](https://raw.githubusercontent.com/theadammurphy/matplotlib_articles/master/imshow/final_html/img/img6.png)
Wait, what?
Don’t worry, you haven’t done anything wrong! You created a grayscale image but matplotlib applied a colormap to it automatically. This happens even if you pick the second or third dimension in your definition.
To make it grayscale, set the keyword argument to in .
![[Image: img7.png]](https://raw.githubusercontent.com/theadammurphy/matplotlib_articles/master/imshow/final_html/img/img7.png)
Perfect! Now you can easily turn any color image into a grayscale one in matplotlib. But there are many more colors you can choose from. Let’s look at all the colormaps in detail.
Matplotlib Imshow Colormap
There are many different colormaps you can apply to your images. Simply pass the name to the keyword argument in and you’re good to go. I recommend you play around with them since most of the names won’t mean anything to you on first reading.
The default colormap is viridis. Let’s explicitly apply it to our cat image.
![[Image: img8.png]](https://raw.githubusercontent.com/theadammurphy/matplotlib_articles/master/imshow/final_html/img/img8.png)
I could write a whole article on colormaps – in fact, the creator of Seaborn did. It’s a fascinating topic. Choosing the right one can make your plot incredible and choosing a bad one can ruin it.
For beginners, matplotlib recommends the colormaps as they work well and are easy on the eyes. Here’s a list of them all.
You’ve already seen, so let’s display the others on a 2×2 grid (if you don’t know how to make subplots in matplotlib check out my article).
![[Image: img9.png]](https://raw.githubusercontent.com/theadammurphy/matplotlib_articles/master/imshow/final_html/img/img9.png)
The colormaps are quite similar and easy to look at. They don’t distort the image and the highs and lows can still be seen.
Here are some other colormaps I’ve randomly chosen. Note how odd some of them are, especially!
![[Image: img10.png]](https://raw.githubusercontent.com/theadammurphy/matplotlib_articles/master/imshow/final_html/img/img10.png)
Matplotlib Imshow Colorbar
If your image has a colormap applied to it, you may want to tell the reader what each color means by providing a colorbar. Use to add one.
![[Image: img11.png]](https://raw.githubusercontent.com/theadammurphy/matplotlib_articles/master/imshow/final_html/img/img11.png)
Remember that every value in an array representing a colored image is a number between 0 and 255 and so the colorbar reflects this.
Note that I called on the first line. Unlike , you can modify images after calling and the changes will be applied to the output.
Matplotlib Imshow Size
As with every in matplotlib, you can manually set the ‘s size. Simply call at the top and set the argument. You can either set it to a specific size in inches or set the aspect ratio:
Note: the default figsize can be found by calling and is inches.
Let’s see what happens when we modify the size of our cat image.
![[Image: img12.png]](https://raw.githubusercontent.com/theadammurphy/matplotlib_articles/master/imshow/final_html/img/img12.png)
This is certainly smaller than the original. But it doesn’t look like it is 1×1 inches – it’s clearly wider that it is tall. But it looks like one of the sides is correct which is better than nothing.
Let’s try to make it 1 inch wide and 3 inches tall.
![[Image: img13.png]](https://raw.githubusercontent.com/theadammurphy/matplotlib_articles/master/imshow/final_html/img/img13.png)
Now the image hasn’t changed at all. What’s going on?
When you resize images in most programs like Microsoft Word or Google Sheets, they maintain their original height/width ratios. This is called preserving the aspect ratio of the image and is usually a very good thing. It ensures that pixels in the image are not distorted when you resize them. This is also the default behavior in matplotlib.
If you actually want to distort the image and to fill up the entire space available in the, change the keyword argument in .
There are only two possible values for:
Let’s look at the same examples as above but set.
![[Image: img14.png]](https://raw.githubusercontent.com/theadammurphy/matplotlib_articles/master/imshow/final_html/img/img14.png)
Great! Now, the image is clearly 1×1 inches but it has come at the expense of slight image distortion.
Let’s distort it even further.
![[Image: img15.png]](https://raw.githubusercontent.com/theadammurphy/matplotlib_articles/master/imshow/final_html/img/img15.png)
Perfect! Clearly the image is distorted but now you know how to do it.
Note the default aspect value can be changed for all images by setting.
Summary
Now you know the basics of displaying images with matplotlib.
You understand why you need to first read images in from a file and store them as a numpy array (NumPy tutorial here) before displaying them with. You know how to read in different types of image files (hint – if it’s a still image, it’s the same process regardless of the file type!).
If you want to set a specific colormap, you use the keyword argument and you can also turn your color image to a grayscale one. Finally, you can add a colorbar and change the size of your image to be anything you want.
There are some more advanced topics to learn about such as interpolation, adding legends and using log scales for your axes but I’ll leave them for another article.
Where To Go From Here?
Do you wish you could be a programmer full-time but don’t know how to start?
Check out the pure value-packed webinar where Chris – creator of Finxter.com – teaches you to become a Python freelancer in 60 days or your money back!
https://tinyurl.com/become-a-python-freelancer
It doesn’t matter if you’re a Python novice or Python pro. If you are not making six figures/year with Python right now, you will learn something from this webinar.
These are proven, no-BS methods that get you results fast.
This webinar won’t be online forever. Click the link below before the seats fill up and learn how to become a Python freelancer, guaranteed.
https://tinyurl.com/become-a-python-freelancer
WordPress conversion from imshow.ipynb by nb2wp v0.3.1
I always struggled with the
Code:
plt.imshow()As you study the resource, you can play my explainer video that leads you through the code in this article:
To show an image in matplotlib, first read it in using
Code:
plt.imread()Code:
plt.imshow()Code:
import matplotlib.pyplot as plt cat_img = plt.imread('Figures/cat.jpeg')
plt.imshow(cat_img)![[Image: img0.png]](https://raw.githubusercontent.com/theadammurphy/matplotlib_articles/master/imshow/final_html/img/img0.png)
To turn the (annoying) axis ticks off, call
Code:
plt.axis('off')Code:
cat_img = plt.imread('Figures/cat.jpeg')
plt.axis('off')
plt.imshow(cat_img)![[Image: img1.png]](https://raw.githubusercontent.com/theadammurphy/matplotlib_articles/master/imshow/final_html/img/img1.png)
Much better! But there is a lot more you can do than just show images. Let’s look at how this works in more detail.
Matplotlib Imshow Example
When you display an in image in matplotlib, there are 2 steps you need to take: first you read the image and then you show it.
You read in the image using
Code:
plt.imread()Code:
Figures/Code:
cat.jpegCode:
Figures/I store the output of
Code:
plt.imread()Code:
plt.imshow()Code:
cat_img = plt.imread('Figures/cat.jpeg')Images are made up of pixels and each pixel is a dot of color. The cat image is 1200×800 pixels. When an image is loaded into a computer, it is saved as an array of numbers. Each pixel in a color image is made up of a Red, Green and Blue (RGB) part. It can take any value between 0 and 255 with 0 being the darkest and 255 being the brightest. In a grayscale image, each pixel is represented by just a single number between 0 and 1. If a pixel is 0, it is completely black, if it is 1 it is completely white. Everything in between is a shade of gray.
So, if the cat image was black and white, it would be a 2D numpy array with shape
Code:
(800, 1200)Code:
(800, 1200, 3)Note that Numpy writes image sizes is the opposite way to matplotlib and the ‘real world’. Numpy is from the world of mathematics and matrices where you always write the number of rows (height) first followed by the columns (width). If you need a quick NumPy refresher, check out my in-depth NumPy tutorial on this blog.
Matplotlib bases its image functions on the ‘real world’ and so displays the width first followed by the height.
Let’s look at the type and size of
Code:
cat_imgCode:
cat_img = plt.imread('Figures/cat.jpeg') print(type(cat_img))
# <class 'numpy.ndarray'> print(cat_img.shape)
# (800, 1200, 3) <class 'numpy.ndarray'>
(800, 1200, 3)Once you’ve read your image into a numpy array, it’s time to display it using
Code:
plt.imshow()Code:
plt.show()Code:
plt.show()Code:
plt.imshow(cat_img)![[Image: img2.png]](https://raw.githubusercontent.com/theadammurphy/matplotlib_articles/master/imshow/final_html/img/img2.png)
I’m not sure why but, by default, all images are shown with axis ticks and labels. This can be quite annoying, so call
Code:
plt.axis('off')However, ticks and labels can be helpful if you only want to select part of the image. By using them as guides, I’ll slice
Code:
cat_imgCode:
# Slicing found by using axis ticks and lables on image above
cat_head = cat_img[150:450, 275:675, :]
plt.imshow(cat_head)![[Image: img3.png]](https://raw.githubusercontent.com/theadammurphy/matplotlib_articles/master/imshow/final_html/img/img3.png)
The above image is a jpeg but you can also display other types of image files such as pngs.
Code:
# A png image
snow_img = plt.imread('Figures/snowboarder.png')
plt.axis('off')
plt.imshow(snow_img)![[Image: img4.png]](https://raw.githubusercontent.com/theadammurphy/matplotlib_articles/master/imshow/final_html/img/img4.png)
You can display gifs but it is slightly more complicated to do so and so is outside the scope of this article. If you read in and show a gif using the above steps, matplotlib will just show one of its frames.
Code:
janice = plt.imread('Figures/janice.gif')
plt.axis('off')
plt.imshow(janice)![[Image: img5.png]](https://raw.githubusercontent.com/theadammurphy/matplotlib_articles/master/imshow/final_html/img/img5.png)
Matplotlib Imshow Grayscale
You can turn any color image into a grayscale image in matplotlib. Since grayscale images are 2D numpy arrays, use slicing to turn your 3D array into a 2D one.
Code:
cat_img = plt.imread('Figures/cat.jpeg') # Turn 3D array into 2D by selecting just one of the three dimensions
grayscale_cat = cat_img[:, :, 0]If you write
Code:
cat_img[:, :, 1]Code:
cat_img[:, :, 2]Let’s plot it and see what happens.
Code:
plt.axis('off')
plt.imshow(grayscale_cat)![[Image: img6.png]](https://raw.githubusercontent.com/theadammurphy/matplotlib_articles/master/imshow/final_html/img/img6.png)
Wait, what?
Don’t worry, you haven’t done anything wrong! You created a grayscale image but matplotlib applied a colormap to it automatically. This happens even if you pick the second or third dimension in your
Code:
grayscale_catTo make it grayscale, set the
Code:
cmapCode:
'gray'Code:
plt.imshow()Code:
plt.axis('off')
plt.imshow(grayscale_cat, cmap='gray')![[Image: img7.png]](https://raw.githubusercontent.com/theadammurphy/matplotlib_articles/master/imshow/final_html/img/img7.png)
Perfect! Now you can easily turn any color image into a grayscale one in matplotlib. But there are many more colors you can choose from. Let’s look at all the colormaps in detail.
Matplotlib Imshow Colormap
There are many different colormaps you can apply to your images. Simply pass the name to the
Code:
cmapCode:
plt.imshow()The default colormap is viridis. Let’s explicitly apply it to our cat image.
Code:
# Turn cat_img into a 2D numpy array colored_cat = cat_img[:, :, 0]
plt.axis('off')
plt.imshow(colored_cat, cmap='viridis')![[Image: img8.png]](https://raw.githubusercontent.com/theadammurphy/matplotlib_articles/master/imshow/final_html/img/img8.png)
I could write a whole article on colormaps – in fact, the creator of Seaborn did. It’s a fascinating topic. Choosing the right one can make your plot incredible and choosing a bad one can ruin it.
For beginners, matplotlib recommends the
Code:
Perceptually Uniform SequentialCode:
percp_uniform_seq_cmaps = ['viridis', 'plasma', 'inferno', 'magma', 'cividis']You’ve already seen
Code:
viridisCode:
fig, axes = plt.subplots(nrows=2, ncols=2)
cmaps = ['plasma', 'inferno', 'magma', 'cividis'] for ax, cmap in zip(axes.flat, cmaps): ax.imshow(colored_cat, cmap=cmap) ax.set(title=cmap) ax.axis('off')
plt.show()![[Image: img9.png]](https://raw.githubusercontent.com/theadammurphy/matplotlib_articles/master/imshow/final_html/img/img9.png)
The colormaps are quite similar and easy to look at. They don’t distort the image and the highs and lows can still be seen.
Here are some other colormaps I’ve randomly chosen. Note how odd some of them are, especially
Code:
flagCode:
fig, axes = plt.subplots(nrows=2, ncols=3, figsize=plt.figaspect(1/2))
cmaps = ['jet', 'binary', 'RdYlGn', 'twilight', 'Pastel1', 'flag'] for ax, cmap in zip(axes.flat, cmaps): ax.imshow(colored_cat, cmap=cmap, label=cmap) ax.set(title=cmap) ax.axis('off')
plt.show()![[Image: img10.png]](https://raw.githubusercontent.com/theadammurphy/matplotlib_articles/master/imshow/final_html/img/img10.png)
Matplotlib Imshow Colorbar
If your image has a colormap applied to it, you may want to tell the reader what each color means by providing a colorbar. Use
Code:
plt.colorbar()Code:
plt.imshow(colored_cat, cmap='hot')
plt.axis('off')
plt.colorbar()![[Image: img11.png]](https://raw.githubusercontent.com/theadammurphy/matplotlib_articles/master/imshow/final_html/img/img11.png)
Remember that every value in an array representing a colored image is a number between 0 and 255 and so the colorbar reflects this.
Note that I called
Code:
plt.imshow()Code:
plt.show()Code:
plt.imshow()Matplotlib Imshow Size
As with every
Code:
FigureCode:
FigureCode:
plt.figure()Code:
figsize- – 8 inches wide, 6 inches tallCode:
plt.figure(figsize=(8, 6))
- – aspect ratio of 2, i.e. twice as tall as it is wide.Code:
plt.figure(figsize=plt.figaspect(2))
Note: the default figsize can be found by calling
Code:
plt.rcParams['figure.figsize']Code:
(6.4, 4.8)Let’s see what happens when we modify the
Code:
FigureCode:
# 1 inch wide x 1 inch tall...?
plt.figure(figsize=(1, 1)) cat_img = plt.imread('Figures/cat.jpeg')
plt.axis('off')
plt.imshow(cat_img)![[Image: img12.png]](https://raw.githubusercontent.com/theadammurphy/matplotlib_articles/master/imshow/final_html/img/img12.png)
This is certainly smaller than the original. But it doesn’t look like it is 1×1 inches – it’s clearly wider that it is tall. But it looks like one of the sides is correct which is better than nothing.
Let’s try to make it 1 inch wide and 3 inches tall.
Code:
# 1 inch wide x 3 inches tall...?
plt.figure(figsize=(1, 3)) cat_img = plt.imread('Figures/cat.jpeg')
plt.axis('off')
plt.imshow(cat_img)![[Image: img13.png]](https://raw.githubusercontent.com/theadammurphy/matplotlib_articles/master/imshow/final_html/img/img13.png)
Now the image hasn’t changed at all. What’s going on?
When you resize images in most programs like Microsoft Word or Google Sheets, they maintain their original height/width ratios. This is called preserving the aspect ratio of the image and is usually a very good thing. It ensures that pixels in the image are not distorted when you resize them. This is also the default behavior in matplotlib.
If you actually want to distort the image and to fill up the entire space available in the
Code:
FigureCode:
aspectCode:
plt.imshow()There are only two possible values for
Code:
aspect- ‘equal’ – (default) preserves the aspect ratio, i.e. the side lengths of the pixels are equal (square)
- ‘auto’ – does not preserve the aspect ratio
Let’s look at the same examples as above but set
Code:
aspect='auto'Code:
# 1 inch wide x 1 inch tall (definitely)
plt.figure(figsize=(1, 1)) cat_img = plt.imread('Figures/cat.jpeg')
plt.axis('off')
plt.imshow(cat_img, aspect='auto')![[Image: img14.png]](https://raw.githubusercontent.com/theadammurphy/matplotlib_articles/master/imshow/final_html/img/img14.png)
Great! Now, the image is clearly 1×1 inches but it has come at the expense of slight image distortion.
Let’s distort it even further.
Code:
# 1 inch wide x 3 inches tall (definitely)
plt.figure(figsize=(1, 3)) cat_img = plt.imread('Figures/cat.jpeg')
plt.axis('off')
plt.imshow(cat_img, aspect='auto')![[Image: img15.png]](https://raw.githubusercontent.com/theadammurphy/matplotlib_articles/master/imshow/final_html/img/img15.png)
Perfect! Clearly the image is distorted but now you know how to do it.
Note the default aspect value can be changed for all images by setting
Code:
plt.rcParams['image.aspect'] = 'auto'Summary
Now you know the basics of displaying images with matplotlib.
You understand why you need to first read images in from a file and store them as a numpy array (NumPy tutorial here) before displaying them with
Code:
plt.imshow()If you want to set a specific colormap, you use the
Code:
cmapThere are some more advanced topics to learn about such as interpolation, adding legends and using log scales for your axes but I’ll leave them for another article.
Where To Go From Here?
Do you wish you could be a programmer full-time but don’t know how to start?
Check out the pure value-packed webinar where Chris – creator of Finxter.com – teaches you to become a Python freelancer in 60 days or your money back!
https://tinyurl.com/become-a-python-freelancer
It doesn’t matter if you’re a Python novice or Python pro. If you are not making six figures/year with Python right now, you will learn something from this webinar.
These are proven, no-BS methods that get you results fast.
This webinar won’t be online forever. Click the link below before the seats fill up and learn how to become a Python freelancer, guaranteed.
https://tinyurl.com/become-a-python-freelancer
WordPress conversion from imshow.ipynb by nb2wp v0.3.1

