How to plot Histogram in Python
In this article, we will see how to plot histogram in Python using Matplotlib.
#1 : Install Matplotlib
If you don’t have Matplotlib installed in your system, use following command to do so.
pip install matplotlib
#2 : Collect the data for the histogram
For example, let’s say that you have the following data about the age of 100 individuals:
Age |
1,1,2,3,3,5,7,8,9,10, 10,11,11,13,13,15,16,17,18,18, 18,19,20,21,21,23,23,24,25,25, 25,25,26,26,26,27,27,27,27,27, 29,30,30,31,33,34,34,34,35,36, 36,37,37,38,38,38,40,41,41,42, 43,44,45,45,46,47,48,48,49,50, 51,52,53,54,55,55,55,57,58,60, 61,62,64,65,66,68,70,71,72,74, 75,77,81,83,84,85,89,90,90,99 |
#3: Determine the number of bins
Next determine the number of bins to be used for the histogram.
For ease of understanding, let’s set the number of bins to 10.
#4 : Plot the histogram in Python using matplotlib
import matplotlib.pyplot as plt x = [1,1,2,3,3,5,7,8,9,10, 10,11,11,13,13,15,16,17,18,18, 18,19,20,21,21,23,23,24,25,25, 25,25,26,26,26,27,27,27,27,27, 29,30,30,31,33,34,34,34,35,36, 36,37,37,38,38,38,40,41,41,42, 43,44,45,45,46,47,48,48,49,50, 51,52,53,54,55,55,55,57,58,60, 61,62,64,65,66,68,70,71,72,74, 75,77,81,83,84,85,89,90,90,99] plt.hist(x, bins=10) plt.show()
Output:
Subscribe
Login
Please login to comment
0 Discussion