How to plot a histogram with various variables in Matplotlib in Python?
Last Updated :
04 Jan, 2022
In this article, we are going to see how to plot a histogram with various variables in Matplotlib using Python.
A histogram is a visual representation of data presented in the form of groupings. It is a precise approach for displaying numerical data distribution graphically. It's a type of bar plot in which the X-axis shows bin ranges and the Y-axis represents frequency. In python, we plot histogram using plt.hist() method.
Syntax: matplotlib.pyplot.hist(x, bins=None, range=None, density=False, weights=None, cumulative=False, bottom=None, histtype=’bar’, align=’mid’, orientation=’vertical’, rwidth=None, log=False, color=None, label=None, stacked=False, \*, data=None, \*\*kwargs)
Simple histogram
The below code is to plot a simple histogram with no extra modifications. packages are imported, CSV file is read and the histogram is plotted using plt.hist() method.
To download and read the CSV file click schoolimprovement2010grants
Python3
# import all modules
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
# Read in the DataFrame
df = pd.read_csv('schoolimprovement2010grants.csv')
# creating a histogram
plt.hist(df['Award_Amount'])
plt.show()
Output:

Example 1: Histogram with various variables
The below histogram is plotted with the use of extra parameters such as bins, alpha, and color. alpha determines the transparency, bins determine the number of bins and color represents the color of the histogram.
Python3
# import all modules
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
# Read in the DataFrame
df = pd.read_csv('schoolimprovement2010grants.csv')
# plotting histogram
plt.hist(df['Award_Amount'],bins = 35,
alpha = 0.45, color = 'red')
plt.show()
Output:

Example 2: Overlapping histograms
In the below code we plot two histograms on the same axis. we use plt.hist() method twice and use the parameters, bins, alpha, and colour just like in the previous example.
To download and view the CSV file used click here.
Python3
# import all modules
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
# Read in the DataFrame
df = pd.read_csv('homelessness.csv')
# plotting two histograms on the same axis
plt.hist(df['individuals'], bins=25, alpha=0.45, color='red')
plt.hist(df['family_members'], bins=25, alpha=0.45, color='blue')
plt.title("histogram with multiple \
variables (overlapping histogram)")
plt.legend(['individuals who are homeless',
'family members who are homeless'])
plt.show()
Output:

Example 3: Plotting three histograms on the same axis
plt.hist() method is used multiple times to create a figure of three overlapping histograms. we adjust opacity, color, and number of bins as needed. Three different columns from the data frame are taken as data for the histograms.
To view or download the CSV file used click medals_by_country_2016
Python3
# import all modules
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
# Read in the DataFrame
df = pd.read_csv('medals_by_country_2016.csv')
# plotting three histograms on the same axis
plt.hist(df['Bronze'],bins = 25, alpha = 0.5,
color = 'red')
plt.hist(df['Gold'],bins = 25, alpha = 0.5,
color = 'blue')
plt.hist(df['Silver'],bins = 25, alpha = 0.5,
color = 'yellow')
plt.title("histogram with multiple \
variables (overlapping histogram)")
plt.legend(['Bronze','Gold','Silver'])
plt.show()
Output:
Similar Reads
How to plot two histograms together in Matplotlib? Creating the histogram provides the visual representation of data distribution. By using a histogram we can represent a large amount of data and its frequency as one continuous plot. How to plot a histogram using Matplotlib For creating the Histogram in Matplotlib we use hist() function which belon
3 min read
Plot 2-D Histogram in Python using Matplotlib 2D Histogram is used to analyse the relationship among two data variables which has wide range of values.A 2D histogram is very similar like 1D histogram.The class intervals of the data set are plotted on both x and y axis.Unlike 1D histogram, it drawn by including the total number of combinations o
4 min read
How to Change the Transparency of a Graph Plot in Matplotlib with Python? Changing transparency in Matplotlib plots enhances visual clarity, especially when data overlaps. Transparency is controlled using a value between 0 (fully transparent) and 1 (fully opaque). This setting can be applied to elements like lines, bars, scatter points and filled areas either during plot
3 min read
How to Set X-Axis Values in Matplotlib in Python? In this article, we will be looking at the approach to set x-axis values in matplotlib in a python programming language. The xticks() function in pyplot module of the Matplotlib library is used to set x-axis values. Syntax: matplotlib.pyplot.xticks(ticks=None, labels=None, **kwargs) xticks() functio
2 min read
Plotting Histogram in Python using Matplotlib Histograms are a fundamental tool in data visualization, providing a graphical representation of the distribution of data. They are particularly useful for exploring continuous data, such as numerical measurements or sensor readings. This article will guide you through the process of Plot Histogram
6 min read
How to plot a normal distribution with Matplotlib in Python? Normal distribution, also known as the Gaussian distribution, is a fundamental concept in probability theory and statistics. It is a symmetric, bell-shaped curve that describes how data values are distributed around the mean. The probability density function (PDF) of a normal distribution is given b
4 min read