Header Ads

Header ADS

Histogram using Matplotplib Python

Matplotlib : Histogram

A histogram is an approximate representation of the distribution of numerical or categorical data. It was first introduced by Karl Pearson.
A Histogram is looks like a Bar Graph but it is not a Bar Graph .

In this Article we will discuss how to create Histogram using matplotlib.pyplot and also discuss its attributes .


Difference between Histogram and Bar Graph .
Histogram and Bar Graphs are looks like same but the difference between them is , Histogram is used to show a distribution whereas a Bar Graph is use for comparison between two or more entities .

Histogram is useful when you are working with a long array or list .


So let's Code :

# Matplotlib : Dynamic Coding
# Histogram ....

import matplotlib.pyplot as plt

population_age = [22,55,62,45,21,22,34,42,4,2,95,85,55,67,95,65,89,34,54,79]

bins =[0,10,29,30,40,50,60,70,80,90,100]

plt.hist(population_age,bins,histtype='bar',color='cyan',width=3)

plt.show()

Output :

what is bins used for :
"bin" (or "bucket") is  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 are usually specified as consecutive, non-overlapping intervals of a variable. The bins (intervals) must be adjacent, and are often (but not required to be) of equal size.

I have created the bins in the interval of 0 to 10 which means the first bin contains elements from 0 to 9 then 10 to 19 and so on.

Histtype Options .
  • bar
  • barstacked
  • stepfilled
Default is bar ..





Adding Label and title  :

# Matplotlib : Dynamic Coding
# Histogram ....

import matplotlib.pyplot as plt

population_age = [22,55,62,45,21,22,34,42,4,2,95,85,55,67,95,65,89,34,54,79]
bins =[0,10,29,30,40,50,60,70,80,90,100]

plt.hist(population_age,bins,histtype='bar',color='cyan',width=3,log=True)
plt.xlabel('bins')
plt.ylabel('Age')
plt.title('population estimation')

plt.show()

Output :



Using log Attribute :

If True, the histogram axis will be set to a log scale. If log is True and x is a 1D array, empty bins will be filtered out and only the non-empty (n, bins, patches) will be returned.

Default is False

# Matplotlib : Dynamic Coding
# Histogram ....

import matplotlib.pyplot as plt

population_age = [22,55,62,45,21,22,34,42,4,2,95,85,55,67,95,65,89,34,54,79]
bins =[0,10,29,30,40,50,60,70,80,90,100]

plt.hist(population_age,bins,histtype='bar',color='cyan',width=3,log=True)
plt.xlabel('bins')
plt.ylabel('Age')
plt.title('population estimation')

plt.show()

Output :



Multiple Histogram in one Chart :

# Matplotlib : Dynamic Coding
# Histogram ....

import matplotlib.pyplot as plt

player_A_scores = [100,90,110,50,12,33,78,32,0,78,120]
player_B_scores = [12,34,56,98,65,28,57,97,110,87,11]

bins =[0,10,29,30,40,50,60,70,80,90,100]

plt.hist(player_A_scores,bins,histtype='bar',color='cyan',width=3,label='Player A')
plt.hist(player_B_scores,bins,histtype='bar',color='yellow',width=3,label='Player B')
plt.legend()
plt.show()


Output :



For more Information on Matplotlib : Histogram Visit here.

Our previous Matplotlib Article :







Thank you .
This blog is contributed by Yogesh singh .
Please leave a feedback in comment section

 

No comments

Powered by Blogger.