Matplotlib.pyplot.imshow: The Complete Guide

Matplotlib library in Python is a numerical and mathematical extension for the NumPy library. Pyplot is a state-based interface to a Matplotlib module that provides a MATLAB-like interface.


Matplotlib.pyplot.imshow() is a built-in library function that displays the data as an image. The plt.imshow() function creates a 2D image using the data.


matplotlib.pyplot.imshow(X, cmap=None, norm=None, aspect=None, interpolation=None, 
                         alpha=None, vmin=None, vmax=None, origin=None, extent=None, 
                         interpolation_stage=None, filternorm=True, filterrad=4.0, resample=None, 
                         url=None, data=None, **kwargs) 


The matplotlib.pyplot.imshow() function has only one required argument, and others are optional.

x: This is the required argument. This argument takes an array as a value. This array consists of the points. 

cmap: This parameter is used to map the scalar data to color. 

norm: This function is used to normalize the data. The scalar data is normalized between 0 to 1. This normalized data is used as the cmap value. 

aspect: The aspect ratio of the axes is passed in this argument. This argument takes values like “auto”, “equal,” or floating-point numbers. 

interpolation: The interpolation method to be used is passed in this argument. By default, it is given as antialiased. 

interpolation_stage: This is the stage at which the interpolation must be carried out. This argument takes values as ‘data’ and “rgba”. By default, it is passed as data.

alpha: This is an optional argument. This argument takes values from 0 to 1. 0 is used for transparent, and 1 is used for opaque.

vmin, vmax: This argument can only be used when the norm parameter is not used. This vmin and vmax are used along with the default norm to map the scalar data to the color map array cmap.

origin: The origin in which the axes are to be placed is passed in this argument. The origin can be given with a value like ‘upper’ or ‘lower’.

extent: The side in which the extension should be carried out is passed in this argument. Some of the values that this argument accepts are ‘left’, ‘right’, ‘bottom’, and ‘top’. 

filternorm: This is a boolean value. If True, this filters the normalized integer values and corrects the rounding errors. This is by default passed as True.

filterrad: This takes filter radius as value to this argument. 

resample: This argument takes a Boolean value as an argument. If it is passed as True, then the full resampling method is executed. 

url: This sets the url for the created axes image.

Return value

The imshow() function returns an axes imager. The imshow() function creates an axes image from the data passed inside the function and then returns the image as output.

Program for creating an axes image using matplotlib.pyplot.imshow

# importing numpy package
import numpy as np

# Importing matplotlib pyplot as plt
import matplotlib.pyplot as plt

# importing matplotlib LogNorm from colors package
from matplotlib.colors import LogNorm

# Creating dx and dy
dx, dy = 0.015, 0.05

# Creating grid
b, a = np.mgrid[slice(-4, 4 + dy, dy),
 slice(-4, 4 + dx, dx)]
x = (1 - a / 3. + a ** 5 + b ** 5) * np.exp(-a ** 2 - b ** 2)
x = x[:-1, :-1]

# Getting the min and max values from the grid
x_min, x_max = -np.abs(x).max(), np.abs(x).max()

# Passing the grid into the imshow function
img = plt.imshow(x, cmap='Blues', vmin=x_min, vmax=x_max,
 extent=[a.min(), a.max(), b.min(), b.max()],
 interpolation='nearest', origin='lower')
plt.title('Example using imshow function')




In this program, we imported matplotlib.pyplot library for plotting the data as an image. The matplotlib library consists of all the functions for plotting the data points in different types of graphs.

Then, we imported numpy for creating a mesh grid. Finally, we created two variables, dx, and dy, for storing the differential values.

Next, we assigned the minimum and maximum values to a variable.

In the next step, we passed the grid into the imshow function. First, we have given the cmap as blues, then the minimum and maximum values, then passed the extent values.

Then, we displayed the color bar using the colorbar function. Then, we displayed the created image using the show function.

That’s it for this tutorial.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.