Plot histogram in python

2020-02-07 Rupesh Sonkamble Download Code

A histogram is an exact representation of the numerical data distribution.It is different from a bar graph, in the sense that a bar graph relates two variables, but only one relates to a histogram.To construct a histogram, the first step is to "bin" the range of values—that is, divide the entire range of values into a series of intervals — and then count how many values fall into each interval.The bins (intervals) must be adjacent, and are often (but not required to be) of equal size.

To plot histogram of numerical data we need to install below libraries in python:

NUMPY

Numpy is one of the basic libraries that must be mastered for data analysis in Python. It can be used to store and process large matrices, and Numpy provides many advanced numerical programming tools such as matrix data types, vector processing, and precision. The computational library is designed for rigorous digital processing.

MATPLOTLIB

Matplotlib is an amazing visualization library in Python for 2D plots of arrays. Matplotlib is a multi-platform data visualization library built on NumPy arrays and designed to work with the broader SciPy stack. It was introduced by John Hunter in the year 2002.

One of the greatest benefits of visualization is that it allows us visual access to huge amounts of data in easily digestible visuals. Matplotlib consists of several plots like line, bar, scatter, histogram etc.

To install above two libraries you have to enter following commands

C:\Users\rupesh>pip install numpy

C:\Users\rupesh>pip install matplotlib

You can check installation using following imports. If you have not get any errors that means your library installation is succesfull.

 

imports

Now you can execute following code to plot histogram of your data:

 

import numpy as np
from matplotlib import pyplot as plt

data = (30,20,10,32,43,23,54,32,23,54,22,12,60,45,67,54,87,98,67,65,35,65,20,30,30,22,54,67,87,89,76,87,69,76,12,21,34,23,43,53,65,76,87,89,90,12,45,65,76,89,90) 

bins = np.arange(-100, 100, 3) 

plt.xlim([min(data)-5, max(data)+5])

plt.hist(data, bins=bins, alpha=0.5)
plt.title('Histogram')
plt.xlabel('variable X (bin size = 3)')
plt.ylabel('count')

plt.show()

After running this code you get histogram like:

histogram

 

If you have any doubts you can ask me in comment section.....Thank You !!!


About author

Card image cap
Rupesh Sonkamble

I'm software engineer and currently work in the field of iot and home automation.!

-IOT Developer

0 Comments


Scroll to Top