Plotting Images Using Matplotlib Library in Python
Plotting Images Using Matplotlib Library in Python
analyticsvidhya.com/blog/2021/11/plotting-images-using-matplotlib-library-in-python
November 2, 2021
Introduction to Matplotlib
Matplotlib is a widely used data visualization library in python. This article illustrates how
to display, modify and save an image using the ‘matplotlib’ library. We will see how to use
the ‘image’ module as it makes working with images quite easy. We can perform a variety
of operations on an image. We can make modifications like changing the color, size,
cropping the image, etc., and also save the modified image as a new image file.
We will be using the ‘image’ submodule of matplotlib library which supports the basic
operations on images like loading, rescaling, and displaying the image. We will alias it as
‘mpimg’ for convenience.
We will also use the pyplot submodule as it supports the different kinds of plotting.
%matplotlib inline
This magic command displays the plots just below the cells where you have coded your
plotting command.
1/8
Before displaying the image first you need to read the image.
Images are made up of pixels and each pixel is a dot of color. Each pixel in a color image is
made up of 3 colors namely red, green and blue. When these three colors are combined,
you can get any color you want.
For a grayscale (black and white) image, the pixel takes a single value in the range of 0 to
1. In grayscale images, 0 represents the black color and 1 represents the white color. Other
values in the range represent different shades of a gray color.
2/8
It’s a 32 bit PNG image having RGBA format (8 bits for each channel which means 32 bits
for each pixel ). It is a 4-channel format where R stands for Red color, G stands for Green
color, B stands for Blue color, and A stands for the alpha channel which describes the
opacity for a color. Opacity can range from 0.0 to 1.0, where 0 stands for fully transparent
and 1 stands for fully opaque.
3/8
You can download this image from the link below.
https://unsplash.com/photos/t0dIdyfHHms
Using ‘imread’ method, we will read the image from the given image file into an array.
It takes two parameters, ‘fname’ and ‘format’. ‘fname’ is the name of the image file. If your
image file is in the same working directory, you can just provide the name of the file, else
you will need to provide the path of the file along with the file name.
‘format’ is the format of the image file. Providing the format is optional. When the format
is not provided, it’s extracted from the name of the image file.
This function returns a multidimensional NumPy array. The shape of the returned
NumPy array depends on the type of the image, that is, whether the image is grayscale,
RGB, or RGBA (RGB with an alpha channel to specify the opacity for the color).
The following code will display the shape of the image data.
print(img.shape)
(853, 640, 4)
Each pixel of the image is represented by an inner list. Since we have an RGBA image,
there are 4 values in each of the inner lists. The first value represents the value of the Red
color channel, the second value represents the value of the Green color channel, the third
value represents the Blue color channel and the fourth value represents the alpha channel
for opacity.
The data type in the lists is float32 because matplotlib rescales the 8-bit data from each
channel to floating-point data between 0.0 and 1.0. For RGB and RGBA images,
matplotlib supports both float32 and uint8 data types. For grayscale images, matplotlib
supports only float32.
4/8
#Displaying the image
imgplot = plt.imshow(img)
Axes are helpful if you want to select only a part of the image. You can use them as a guide
while slicing the image.
For the given image, if you want to display only the TajMahal excluding the other parts of
the image then you can use the ticks and labels displayed on the axes to decide which part
of the image to slice. The third parameter shows the color channels. Here, we are selecting
all the color channels.
5/8
imgplot = plt.imshow(img)
plt.colorbar()
plt.axis("off")
plt.imshow(img)
After executing the code, we can see that the axes are gone
in the above image.
grayscale_img = img[:, :, 0]
plt.imshow(grayscale_img, cmap='gray')
Saving an Image
You can apply different pseudocolor schemes to the image. It makes it easy to visualize
the image data by enhancing the contrast. If you don’t provide a colormap value, the
default value ‘viridis’ is used. Let’s see how it affects the image by executing the following
code.
plt.imshow(grayscale_img)
6/8
There are many colormap values that you can choose
and apply to your image. For more information on
colormap values you can visit
https://matplotlib.org/stable/tutorials/colors/colorm
aps.html
plt.imshow(grayscale_img,cmap ='hot')
plt.savefig("New_Image.png")
7/8
Let’s create and plot a histogram of the grayscale image that we created above using the
following code.
plt.hist(img.ravel())
References
https://matplotlib.org/stable/tutorials/introductory/images.html
Endnote
We learned how to use the ‘image’ module of matplotlib to perform Various operations on
the image including turning an image into a grayscale image, selecting a part of the image,
displaying the color bar to the image, applying different color schemes to the image and
saving the modified image. We also learned how to plot the histogram of the image data.
The media shown in this article is not owned by Analytics Vidhya and are
used at the Author’s discretion.
8/8