How to Plot Histogram from List of Data in Matplotlib?
Last Updated :
05 Aug, 2024
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 variable that holds a set of data. It can be a list or a column in a DataFrame etc.
- bins- It can be used to group the data in the DataVariable. It can accept an Integer or a list of numbers.
- edgecolor- It gives an edge color to each bin i.e., Each bar in histogram.
We can plot the histogram using data in the list by passing a list to hist method.
Example 1:
In this example, we passed the list to hist method to plot histogram from list of data. Here we didn't specify edgecolor and bins parameters in hist method. So in the above output, we didn't find any edge color to any bar and we got a random number of bins (bars).
Python
# import necessary packages
import matplotlib.pyplot as plt
# list of weights
weights = [30, 50, 45, 80, 76, 55,
45, 47, 50, 65]
# plotting labelled histogram
plt.hist(weights)
plt.xlabel('weight')
plt.ylabel('Person count')
plt.show()
Output:

Example 2:
To the same data that we used in the above example, we will specify bins and edgecolor additionally in this example.
Python
# import necessary packages
import matplotlib.pyplot as plt
# list of weights
weights = [30, 50, 45, 80, 76, 55,
45, 47, 50, 65]
# plotting labelled histogram
plt.hist(weights, bins=5, edgecolor='black')
plt.xlabel('weight')
plt.ylabel('Person count')
plt.show()
Output:

As we specified bin count as 5 we got 5 bars in the resultant plot and edgecolor helps us to distinguish bars between them if they are close to each other as the above scenario.
Example 3:
We can also pass list of values as an argument to bins parameter in hist method. It helps us to filter the data from the list.
Python
# import necessary packages
import matplotlib.pyplot as plt
# list of weights
weights = [30, 50, 45, 80, 76, 55,
45, 47, 50, 65]
# list of bins
bins = [30, 40, 50, 60]
# plotting labelled histogram
plt.hist(weights, bins=bins, edgecolor='black')
plt.xlabel('weight')
plt.ylabel('Person count')
plt.show()
Output:

As we passed the list of bins that contains 30,40,50,60 to bins parameter, so while plotting histogram from list of data python only considers the data that are in the range of specified bins. so if the data value exceeds the range then that data point is not considered while plotting.
In the given list of data- 65,76,80 exceeds the bin range so, those data points are not considered during plotting the graph.
Similar Reads
How to Plot List of X, Y Coordinates in Matplotlib? Prerequisites: Matplotlib numpy Matplotlib is a comprehensive library for creating static, animated, and interactive visualizations in Python. To plot any data the most basic step starts with creating or loading it, this article discusses all the ways of passing the data to be plotted as list. Whi
2 min read
How to plot a Histogram in MATLAB ? A Histogram is a diagrammatic representation of a group of data over user-specified ranges. Basically, the histogram contains several bins. Bins are non-overlapping intervals in which the data is spread. In MATLAB we have a function named hist() which allows us to plot a bar graph. Syntax: hist(X)
2 min read
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
How to Add Text Labels to a Histogram in Plotly Plotly is a powerful and versatile library for creating interactive visualizations in Python. Among its many features, Plotly allows users to create histograms, which are essential for visualizing the distribution of numerical data. Adding text labels to histograms can enhance the interpretability o
3 min read
Adding Labels to Histogram Bars in Matplotlib In Matplotlib, a histogram displays data distribution across defined intervals, known as bins. Each bar in the histogram represents the frequency or density of data points within these bins. To communicate these values, you can add labels to the bars by placing text directly on or above them, showin
3 min read