We can plot a histogram matplotlib in Python using hist() function. This hist() has various parameters like bins, range, array sequence etc. In this article, We will learn how to plot a histogram matplotlib in Python with the easiest example. We will also various parameters of hist() function with their impact.
Steps to Plot a histogram matplotlib :
Let’s understand this plotting in steps for better understanding. We will understand this in three easy following steps.
Step 1: Import required library
Firstly, In this step, we will only import the necessary python modules for plotting histograms. We will import matplotlib.pyplot.
import matplotlib.pyplot as plt
Step 2: Dataset Creating –
Secondly, We need a dataset for plotting a histogram. Just for simplicity, we are using a list of integer numbers with the minimum element as 12 and a max as 100.
array = [23,12,45,67,86,35,67,54,25,44,56,99,100,76,62,63,64,41]
Step 3: Plotting histogram –
Further, In this step, we will plot the histogram. We will call the hist() function. In the hist() function, We will only parameterize the required parameter. Rest parameters are default None.
plt.hist(array , bins=5, facecolor='green', alpha=0.7) plt.show()
Here we have used the below parameter for customizing the histogram chart.
3.1 array – sequence of the dataset.
3.2 bins- It is an integer number that decides the intervals in the histogram.
3.3 facecolor- color for the graph and data points.
3.4 alpha – it ranges from (0 -1)
There are so many parameters available in the hist() function. Like range, density, weight, etc are set None by default. We can parameterize them as per our requirements only.
Complete code for Histogram python-
To sum up, Once we put all code together and execute it. We get the following histogram.
import matplotlib.pyplot as plt #dataset as array array = [23,12,45,67,86,35,67,54,25,44,56,99,100,76,62,63,64,41] #plotting histogram plt.hist(array, bins=5, facecolor='green', alpha=0.7) plt.show()
The histogram is one the best way to show the data variations. It is self-explanatory and easily explainable. Well, I think now plotting a histogram with Matplotlib is easy for you. If you have any suggestions over this implementation, please let us know.
Data Science Learner Team
Join our list
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.