0% found this document useful (0 votes)
8 views

What is a Histogram in Matplotlib

A histogram in Matplotlib is a graphical representation of data distribution, using a bar graph where the x-axis represents bin ranges and the y-axis represents frequency. The matplotlib.pyplot.hist() function is used to create histograms, allowing for various parameters such as bin count, density, and color customization. Examples demonstrate how to plot histograms with default parameters, probability densities, and custom bin counts.

Uploaded by

xaxinfos
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

What is a Histogram in Matplotlib

A histogram in Matplotlib is a graphical representation of data distribution, using a bar graph where the x-axis represents bin ranges and the y-axis represents frequency. The matplotlib.pyplot.hist() function is used to create histograms, allowing for various parameters such as bin count, density, and color customization. Examples demonstrate how to plot histograms with default parameters, probability densities, and custom bin counts.

Uploaded by

xaxinfos
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 5

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.

Parameters of Histogram in Matplotlib


To make a histogram, establish a bin for the ranges, divide the entire range of values into a series
of intervals and count the values that fall into each interval. Bins are defined as non-overlapping,
successive intervals of variables. To compute and create a histogram of x, use the
matplotlib.pyplot.hist() function.

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

How to Create a Plot Histogram in Matplotlib?


Matplotlib is a Python toolkit for plotting visuals that includes several useful formatting and
plotting features. The hist() function in matplotlib pyplot can be used to plot a histogram.

The syntax is as follows:

import matplotlib.pyplot as plot


plot.hist(x)
plot.show()
The variable you wish to create a histogram is represented by x, an array or sequence of
numbers. The bins option can define the number of bins or bin edges wanted in the graphic (see
the examples below).

Examples:

Let us look at several examples of plotting a histogram with the hist() method.

1. Histogram with default parameters in matplotlib Imagine we would want to make a


histogram of 100 students' grades in a high school math class. We can use the hist() method from
matplotlib pyplot. Let us see what we get using just the default parameters.

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]

# display the histogram


plot.hist(math__score)
plot.show()

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]

# display the histogram


plot.hist(math__score)
# adding format to the histogram
plot.xlabel("_Scores")
plot.ylabel("_Students")
plot.title("Scores in the Math class histogram")

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.

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]

# display the histogram


plot.hist(math__score, density=True)
# adding format to the histogram
plot.xlabel("_Scores")
# plot.ylabel("_Students")
plot.title("Scores in the Math class histogram")
plot.show()

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)

3. Histogram with custom bin counts

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.

If we require the histogram to have 20 bins, for example:

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]

# display the histogram


plot.hist(math__score, bins=20)
# adding format to the histogram
plot.xlabel("_Scores")
plot.ylabel("_Students")
plot.title("Scores in the Math class histogram")
plot.show()

You might also like