What is a Histogram in Matplotlib
What is a Histogram in Matplotlib
A histogram is a visual representation of data that is organized into groups. It is a precise way of
displaying numerical data distribution graphically. It is a bar graph with the x-axis representing
bin ranges and the y-axis representing frequency.
The parameters are supported by the matplotlib.pyplot.hist() method are listed in the table below:
Attribute Parameter
x sequence of arrays or arrays themselves
density Boolean values are contained in the optional parameter.
range An optional parameter represents the upper and lower range of bins.
weights This parameter can be an integer, a sequence, or a string.
The optional parameter contains an array of weights with the same dimensions as the x
bins
bottom location of each bin's baseline.
The type of histogram [bar, bar stacked, step, stepfilled] is an optional parameter; the
histtype
default is "bar."
align An optional parameter controls the histogram plotting [left, right, mid].
color Sets the color or sequence of color specifications as an optional parameter.
rwidth The relative width of the bars in relation to the bin width is an optional parameter.
log Optional parameter for setting the log scale of the histogram axis
a string or a series of strings that can be used to match several datasets as an optional
label
parameter
Examples:
Let us look at several examples of plotting a histogram with the hist() method.
# grades in mathematics
math__score = [54, 37, 57, 43, 72, 43, 91, 43, 93, 49,
60, 69, 62, 73, 42, 63, 82, 83, 81, 33,
70, 50, 81, 69, 87, 45, 85, 55, 53, 60,
53, 66, 53, 79, 79, 69, 55, 87, 66, 49,
73, 41, 75, 60, 90, 50, 44, 47, 78, 79,
78, 48, 84, 68, 56, 64, 83, 48, 86, 63,
59, 87, 42, 68, 47, 98, 52, 72, 62, 66,
83, 47, 34, 84, 64, 98, 95, 57, 82, 52,
56, 74, 84, 83, 65, 31, 89, 75, 79, 77,
78, 68, 78, 48, 88, 62, 88, 74, 74, 78]
This histogram mimics a normal distribution, with many students scoring in the 60 to 80 range (closer to
the mean) and frequency tapering at both ends. To make the above figure more understandable, we
may add some basic formatting, such as axis labels and chart titles.
import matplotlib.pyplot as plot
# grades in mathematics
math__score= [54, 37, 57, 43, 72, 43, 91, 43, 93, 49,
60, 69, 62, 73, 42, 63, 82, 83, 81, 33,
70, 50, 81, 69, 87, 45, 85, 55, 53, 60,
53, 66, 53, 79, 79, 69, 55, 87, 66, 49,
73, 41, 75, 60, 90, 50, 44, 47, 78, 79,
78, 48, 84, 68, 56, 64, 83, 48, 86, 63,
59, 87, 42, 68, 47, 98, 52, 72, 62, 66,
83, 47, 34, 84, 64, 98, 95, 57, 82, 52,
56, 74, 84, 83, 65, 31, 89, 75, 79, 77,
78, 68, 78, 48, 88, 62, 88, 74, 74, 78]
plot.show()
2. Histogram with probability densities instead of frequencies The density parameter, False by default,
can be used to transform the values on the y-axis from frequencies to probabilities, with each bin
indicating its probability density.
# grades in mathematics
math__score= [54, 37, 57, 43, 72, 43, 91, 43, 93, 49,
60, 69, 62, 73, 42, 63, 82, 83, 81, 33,
70, 50, 81, 69, 87, 45, 85, 55, 53, 60,
53, 66, 53, 79, 79, 69, 55, 87, 66, 49,
73, 41, 75, 60, 90, 50, 44, 47, 78, 79,
78, 48, 84, 68, 56, 64, 83, 48, 86, 63,
59, 87, 42, 68, 47, 98, 52, 72, 62, 66,
83, 47, 34, 84, 64, 98, 95, 57, 82, 52,
56, 74, 84, 83, 65, 31, 89, 75, 79, 77,
78, 68, 78, 48, 88, 62, 88, 74, 74, 78]
Each bin in the graph above reflects the "density" of the frequency concentrated in that bin. That
is, for a bin, density equals the number of items inside the bin divided by the number of items
outside the bin (total count x bin width)
The hist() function utilizes ten equal-width bins by default, as shown in the examples above. The
bins argument allows selection of the bin count.
# grades in mathematics
math__score= [54, 37, 57, 43, 72, 43, 91, 43, 93, 49,
60, 69, 62, 73, 42, 63, 82, 83, 81, 33,
70, 50, 81, 69, 87, 45, 85, 55, 53, 60,
53, 66, 53, 79, 79, 69, 55, 87, 66, 49,
73, 41, 75, 60, 90, 50, 44, 47, 78, 79,
78, 48, 84, 68, 56, 64, 83, 48, 86, 63,
59, 87, 42, 68, 47, 98, 52, 72, 62, 66,
83, 47, 34, 84, 64, 98, 95, 57, 82, 52,
56, 74, 84, 83, 65, 31, 89, 75, 79, 77,
78, 68, 78, 48, 88, 62, 88, 74, 74, 78]