# Histogram of an image using matplotlib in Python

In this session, we are going to learn how we can plot the histogram of an image using the matplotlib package in Python for a given image.

## what do you mean by histogram

A histogram is a graphical representation of statistical data that uses rectangles to represent the frequency of the data items. In simple words, we can also say that histogram represents the distribution of pixels of an image on the coordinate system.

Now move on the program:

1st import the all required package :

#important library to show the image import matplotlib.image as mpimg import matplotlib.pyplot as plt #importing numpy to work with large set of data. import numpy as np

write a code to read and show a given image:

#image read function img=mpimg.imread('images.jpg') #image sclicing into 2D. x=img[:,:,0] # x co-ordinate denotation. plt.xlabel("Value") # y co-ordinate denotation. plt.ylabel("pixels Frequency") # title of an image . plt.title("Original Image") # imshow function with comperision of gray level value. plt.imshow(x,cmap="gray") #plot the image on a plane. plt.show()

code for plotting the histogram on the plane:

plt.title("HIstogramm for given Image' ") plt.xlabel("Value") plt.ylabel("pixels Frequency") #hist function is used to plot the histogram of an image. plt.hist(x)

Now combine the whole program:

#important library to show the image import matplotlib.image as mpimg import matplotlib.pyplot as plt #importing numpy to work with large set of data. import numpy as np #image read function img=mpimg.imread('images.jpg') #image sclicing into 2D. x=img[:,:,0] # x co-ordinate denotation. plt.xlabel("Value") # y co-ordinate denotation. plt.ylabel("pixels Frequency") # title of an image . plt.title("Original Image") # imshow function with comperision of gray level value. plt.imshow(x,cmap="gray") #plot the image on a plane. plt.show() plt.title("HIstogramm for given Image' ") plt.xlabel("Value") plt.ylabel("pixels Frequency") #hist function is used to plot the histogram of an image. plt.hist(x)

Output:

first given image:

Histogram of this image:

Also, read:

## Leave a Reply