Codecademy Logo


Matplotlib Function To Create Histogram

In Python, the pyplot.hist() function in the Matplotlib pyplot library can be used to plot a histogram. The function accepts a NumPy array, the range of the dataset, and the number of bins as input.

import numpy as np
from matplotlib import pyplot as plt
# numpy array
data_array = np.array([1,1,1,1,1,2,3,3,3,4,4,5,5,6,7])
# plot histogram
plt.hist(data_array, range = (1,7), bins = 7)

Mean of a Dataset

The mean, or average, of a dataset is calculated by adding all the values in the dataset and then dividing by the number of values in the set.

For example, for the dataset [1,2,3], the mean is 1+2+3 / 3 = 2.

Histogram Bins

In a histogram, the range of the data is divided into sub-ranges represented by bins. The width of the bin is calculated by dividing the range of the dataset by the number of bins, giving each bin in a histogram the same width.

What is a Histogram?

A Histogram is a plot that displays the spread, or distribution of a dataset. In a histogram, the data is split into intervals, called bins. Each bin shows the number of data points that are contained within that bin.

Histogram Bin Count

In a histogram, the bin count is the number of data points that fall within the bin’s range.

Histogram’s X and Y Axis

A histogram is a graphical representation of the distribution of numerical data. In a histogram, the bin ranges are on the x-axis and the counts are on the y-axis.

An example histogram.

The title of the histogram is 'Exercise Class Age Distribution'.

Along the x-axis, labeled 'Ages', the bin ranges are each 10 years, starting with age 20 and ending at age 70. The 5 bin ranges are as follows: 20-30, 30-40, 40-50, 50-60, and 60-70.

Along the y-axis, labeled 'Count', the values are whole numbers.  The values start at 0 and end at 7.

The first bin range, ages 20-30, has a count of 7. The second bin range, ages 30-40, and the third bin range, ages 40-50, each have a count of 4. The fourth bin range, ages 50-60, has a count of 3. The last bin range, ages 60-70, has a count of 2.

Each bin range is the shape of a rectangle starting at count 0 and ending at the bin ranges' respective count. These rectangles are all the same color blue with a black outline. Also, there is no white space between the bin ranges.

Learn More on Codecademy