Hello developers, in this article we will discuss about Plotting Histogram in Python using Matplotlib in Python. We know, histogram is a powerful visualization tool used in the data analysis purpose for understanding the distribution of data. The python’s Matplotlib library offers many capabilities and functionality for creating histograms easily.
Steps to plot a histogram using Matplotlib in Python:
First, install Matplotlib and confirm its installation by running the following code.
pip install matplotlib
Then, import Matplotlib and other libraries for data generation on which plotting will be done.
import matplotlib.pyplot as plt import numpy as np
Select or choose the dataset on which the histogram will be plotted. The following code shows the plotting of the histogram :
plt.hist(data, bins=30, edgecolor='black') # Plotting the histogram with 30 bins only plt.title('Histogram of my Data') plt.xlabel('Value') plt.ylabel('Frequency') plt.show()
Here let us understand the attributes and functions we used here,
So starting with :
data is the dataset on which the graph is to be plotted.
bins is the no of intervals for the histogram.
edgecolor is the color of the edges of bars.
These were the attributes of hist(),
Now let us discuss about the other functions which are used in this program:
title() states the title of the graph.
xlabel() states the x axis label to be plotted in graph.
ylabel() states the y axis label to be plotted in graph.
We can display the histogram using plt.show() but inorder to save the generated histogram in to a file, we can do by using the function, plt.savefig(‘histo.png”).
So, by this way we can easily add histogram in our python code, just by using the matplotlib library.