Title: How to Display a Matplotlib RGB Image - PyImageSearch

URL Source: https://pyimagesearch.com/2014/11/03/display-matplotlib-rgb-image/

Published Time: 2014-11-03T15:00:22+00:00

Markdown Content:
[![Image 1: Displaying Matplotlib RGB images](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20600%20150'%3E%3C/svg%3E)](https://pyimagesearch.com/wp-content/uploads/2014/05/matplotlib-rgb-header.jpg)

So we know that matplotlib is awesome for generating graphs and figures.

But what if we wanted to display a simple RGB image? Can we do that with matplotlib?

Of course!

This blog post will show you how to display a Matplotlib RGB image in only a few lines of code…as well as clear up any caveats that you may run into when using OpenCV and matplotlib together.

![Image 2](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20128%20128'%3E%3C/svg%3E)

#### Looking for the source code to this post?

[Jump Right To The Downloads Section](#download-the-code)

Alright, let’s not waste any time. Let’s jump into some code:

\>>> import matplotlib.pyplot as plt
>>> import matplotlib.image as mpimg
>>> image = mpimg.imread("chelsea-the-cat.png")
>>> plt.imshow(image)
>>> plt.show()

The first thing we are going to do is import our `matplotlib` package. Then we’re going to import the `image` sub-package of `matplotlib`, aliasing it as `mpimg` for convenience. This sub-package handles matplotlib’s image manipulations.

A simple call to the `imread` method loads our image as a multi-dimensional NumPy array (one for each Red, Green, and Blue component, respectively) and `imshow` displays our image to our screen.

We can see our image below:

[![Image 3: Figure 1: Displaying a Matplotlib RGB image (note how the axes are labeled).](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20600%20450'%3E%3C/svg%3E)](https://pyimagesearch.com/wp-content/uploads/2014/05/matplotlib-rgb-with-axis.jpg)

**Figure 1:** Displaying a Matplotlib RGB image (note how the axes are labeled).

That’s a good start, but how about getting rid of the numbered axes?

plt.axis("off")
plt.imshow(image)
plt.show()

By calling `plt.axis("off")` we can remove our numbered axes.

Executing our code we end up with:

[![Image 4: Figure 1: Displaying a Matplotlib RGB image, this time, turning off our axes.](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20600%20450'%3E%3C/svg%3E)](https://pyimagesearch.com/wp-content/uploads/2014/05/matplotlib-rgb-no-axis.jpg)

**Figure 2:** Displaying a Matplotlib RGB image, this time, turning off our axes.

Nothing to it! You can now see that the numbered axes are gone.

The OpenCV Caveat
-----------------

But of course, we use OpenCV a lot on this blog.

So let’s load up an image using OpenCV and display it with matplotlib:

import cv2
image = cv2.imread("chelsea-the-cat.png")
plt.axis("off")
plt.imshow(image)
plt.show()

Again, the code is simple.

But the results aren’t as expected:

[![Image 5: Figure 3: Loading an image with OpenCV and displaying it with matplotlib.](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20600%20450'%3E%3C/svg%3E)](https://pyimagesearch.com/wp-content/uploads/2014/05/matplotlib-rgb-reversed.jpg)

**Figure 3:** Loading an image with OpenCV and displaying it with matplotlib.

Uh-oh. That’s not good.

The colors of our image are clearly wrong!

Why is this?

The answer lies as a caveat with OpenCV.

OpenCV represents RGB images as multi-dimensional NumPy arrays…_**but in reverse order!**_

This means that images are actually represented in BGR order rather than RGB!

There’s an easy fix though.

All we need to do is convert the image from BGR to RGB:

plt.axis("off")
plt.imshow(cv2.cvtColor(image, cv2.COLOR\_BGR2RGB))
plt.show()

Running our script we can see that the colors of our image are now correct:

[![Image 6: Figure 4: When using OpenCV and displaying an image using matplotlib, be sure to call cv2.cvtColor first.](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20600%20450'%3E%3C/svg%3E)](https://pyimagesearch.com/wp-content/uploads/2014/05/matplotlib-rgb-fixed.jpg)

**Figure 4:** When using OpenCV and displaying an image using matplotlib, be sure to call cv2.cvtColor first.

As I said, there’s nothing to displaying matplotlib RGB images!

### What's next? We recommend [PyImageSearch University](https://pyimagesearch.com/pyimagesearch-university/?utm_source=blogPost&utm_medium=bottomBanner&utm_campaign=What%27s%20next%3F%20I%20recommend).

**Course information:**  
84 total classes • 114+ hours of on-demand code walkthrough videos • Last updated: February 2024  
★★★★★ 4.84 (128 Ratings) • 16,000+ Students Enrolled

**I strongly believe that if you had the right teacher you could _master_ computer vision and deep learning.**

Do you think learning computer vision and deep learning has to be time-consuming, overwhelming, and complicated? Or has to involve complex mathematics and equations? Or requires a degree in computer science?

That’s _not_ the case.

All you need to master computer vision and deep learning is for someone to explain things to you in _simple, intuitive_ terms. _And that’s exactly what I do_. My mission is to change education and how complex Artificial Intelligence topics are taught.

If you're serious about learning computer vision, your next stop should be PyImageSearch University, the most comprehensive computer vision, deep learning, and OpenCV course online today. Here you’ll learn how to _successfully_ and _confidently_ apply computer vision to your work, research, and projects. Join me in computer vision mastery.

**Inside PyImageSearch University you'll find:**

*   ✓ **84 courses** on essential computer vision, deep learning, and OpenCV topics
*   ✓ **84 Certificates** of Completion
*   ✓ **114+ hours** of on-demand video
*   ✓ **Brand new courses released _regularly_**, ensuring you can keep up with state-of-the-art techniques
*   ✓ **Pre-configured Jupyter Notebooks in Google Colab**
*   ✓ Run all code examples in your web browser — works on Windows, macOS, and Linux (no dev environment configuration required!)
*   ✓ Access to **centralized code repos for _all_ 536+ tutorials** on PyImageSearch
*   ✓ **Easy one-click downloads** for code, datasets, pre-trained models, etc.
*   ✓ **Access** on mobile, laptop, desktop, etc.

[Click here to join PyImageSearch University](https://pyimagesearch.com/pyimagesearch-university/?utm_source=blogPost&utm_medium=bottomBanner&utm_campaign=What%27s%20next%3F%20I%20recommend)

Summary
-------

In this blog post I showed you how to display matplotlib RGB images.

We made use of `matplotlib`, `pyplot` and `mpimg` to load and display our images.

To remove the axes of the figure, make a call to `plt.axis("off")`.

Just remember that if you are using OpenCV that your images are stored in BGR order rather than RGB!

As long as you remember that, you won’t have any issues!

![Image 7](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20410%20567'%3E%3C/svg%3E)

#### Download the Source Code and FREE 17-page Resource Guide

Enter your email address below to get a .zip of the code and a **FREE 17-page Resource Guide on Computer Vision, OpenCV, and Deep Learning.** Inside you'll find my hand-picked tutorials, books, courses, and libraries to help you master CV and DL!

![Image 8](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20240%20240'%3E%3C/svg%3E)

#### **About the Author**

Hi there, I’m Adrian Rosebrock, PhD. All too often I see developers, students, and researchers wasting their time, studying the wrong things, and generally struggling to get started with Computer Vision, Deep Learning, and OpenCV. I created this website to show you what I believe is the best possible way to get your start.