In this tutorial, we will learn how to use the imshow function in Python. The imshow function is a part of matplotlib, which is a powerful 2D plotting library for Python.
It allows us to visualize images in various formats, such as JPG, BMP, and PNG, directly from our code. The imshow function takes an input image in the form of a 2D or 3D array and displays it on an axes system.
Let’s dive into the steps to use imshow in Python.
Step 1: Install the matplotlib library
First, ensure that you have the matplotlib library installed on your machine. If you do not have it already, you can use the following command to install it using pip:
1 |
pip install matplotlib |
Step 2: Import required libraries
Next, we need to import the necessary libraries for our code. We will need matplotlib.pyplot and NumPy as follows:
1 2 |
import matplotlib.pyplot as plt import numpy as np |
Step 3: Load or create the image
Before we can use imshow to display the image, we first need to load or create the image itself. In this example, we’ll create a simple image using a 2D NumPy array of random values:
1 |
image = np.random.rand(10, 10) |
The above code snippet creates an image of dimensions 10×10, filled with random grayscale values between 0 and 1.
Alternatively, if you want to load an existing image from your file system, you can use the imageio library, which provides a simple interface to read and save image data. First, install the imageio library using pip:
1 |
pip install imageio |
Then, you can load an image as follows:
1 2 3 4 |
import imageio filename = "path/to/your/image.png" image = imageio.imread(filename) |
Step 4: Display the image using imshow
Now that we have our image, we can display it using the imshow function from matplotlib.pyplot:
1 2 |
plt.imshow(image) plt.show() |
The imshow function will display the image array provided as an argument, and “plt.show()” will display the image in a window.
Step 5: Customize the display (optional)
You can further customize the display of the image by using additional options available with imshow. For instance, you can change the color map, apply interpolation, and set axis labels:
1 2 3 4 5 |
plt.imshow(image, cmap='gray', interpolation='nearest') plt.xlabel('X-axis') plt.ylabel('Y-axis') plt.title('Sample Image') plt.show() |
The above code sets the color map to grayscale, applies the nearest-neighbor interpolation, and adds labels to the axis.
Full Code Example
Here is the complete code for displaying a random 10×10 grayscale image using the imshow function:
1 2 3 4 5 6 7 8 9 10 |
import matplotlib.pyplot as plt import numpy as np image = np.random.rand(10, 10) plt.imshow(image, cmap='gray', interpolation='nearest') plt.xlabel('X-axis') plt.ylabel('Y-axis') plt.title('Sample Image') plt.show() |
Output

Conclusion
In this tutorial, we learned how to use the imshow function in Python for displaying images. The imshow function is an essential tool for visualizing and plotting images within your code, whether they are preloaded or generated.
Practice using different customization options and various types of images for a better understanding of the imshow function in Python.