Matplotlib Example | Load and Display an Image

In this matplotlib example, we will introduce how to load and display an image for beginners.

There are three steps, we can do step by step.

Step 1: load python library

import numpy as np 
import matplotlib.cbook as cbook 
import matplotlib.image as image 
import matplotlib.pyplot as plt 

Step 2: open an image to get image data

For example:

with cbook.get_sample_data('img.jpg') as image_file: 
    image = plt.imread(image_file) 

In this matplotlib, we use plt.imread() function to get image data.

Step 3: display image in matplotlib

fig, ax = plt.subplots() 
ax.imshow(image) 
ax.axis('off') 


plt.show() 

Then we will find an image as follows:

Matplotlib Example | Load and Display an Image