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 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
How to Make a Simple Histogram with Altair in Python? Prerequisites: Altair Simple Histogram is the representation of frequency distribution by means of rectangles whose width represents the class interval. Histogram is the graphical representation that organizes grouped data points into the specified range. By using histogram we can visualize large am
4 min read
How to Color Scatterplot by a variable in Matplotlib? In this article, we are going to see how to color scatterplot by variable in Matplotlib. Here we will use matplotlib.pyplot.scatter() methods matplotlib library is used to draw a scatter plot. Scatter plots are widely used to represent relations among variables and how change in one affects the othe
2 min read
How to Create Subplots in Matplotlib with Python? Matplotlib is a widely used data visualization library in Python that provides powerful tools for creating a variety of plots. One of the most useful features of Matplotlib is its ability to create multiple subplots within a single figure using the plt.subplots() method. This allows users to display
6 min read
How to Plot Histogram from List of Data in Matplotlib? In this article, we are going to see how to Plot a Histogram from a List of Data in Matplotlib in Python.The histogram helps us to plot bar-graph with specified bins and can be created using the hist() function.Syntax: hist( dataVariable, bins=x, edgecolor='anyColor' )Parameters:dataVariable- Any va
3 min read
Scatter Plot with Marginal Histograms in Python with Seaborn Prerequisites: Seaborn Scatter Plot with Marginal Histograms is basically a joint distribution plot with the marginal distributions of the two variables. In data visualization, we often plot the joint behavior of two random variables (bi-variate distribution) or any number of random variables. But
2 min read