histogram

Plots a histogram of the data and returns an object with the bins and counts.

Category: Graphics > 2-D and 3-D Plots

Syntax

histogram(X)

Chooses the bins automatically and draws the counts.

histogram(X, nbins)

Uses nbins bins.

histogram(X, edges)

Uses the vector edges as bin edges.

histogram(___, Name, Value)

Sets options such as 'Normalization', 'BinWidth', 'BinLimits' or 'BinMethod'.

h = histogram(___)

Also returns the Histogram object.

Description

Bins are closed on the left and open on the right, except for the last one, which includes both edges. NaN values are ignored. Without options the number and width of bins come from an automatic rule that depends on the data.

The returned object lets you inspect the result numerically or adjust it after the fact, with morebins and fewerbins. To get counts without a plot, use histcounts.

Input arguments

X
Numeric data. It is treated as a vector, whatever its shape.
'Normalization'
'count' (default), 'probability', 'percentage', 'countdensity', 'pdf', 'cumcount' or 'cdf'.
'BinEdges'
Vector of bin edges.
'BinWidth'
Width of every bin.

Output arguments

h
Histogram object. Read h.Values, h.BinEdges, h.NumBins, h.BinWidth and h.BinLimits; changing them redraws the plot.

Examples

Explicit edges

x = [1 2 2 3 3 3 4 4 5 9];
h = histogram(x, [0 2 4 10]);
h.Values
h.BinWidth
Output
ans = 1×3

     1     5     4
ans = 'nonuniform'

Probability normalization

x = [1 2 2 3 3 3 4 4 5 9];
h = histogram(x, 'BinEdges', 0:3:9, 'Normalization', 'probability');
disp(h.Values)
disp(sum(h.Values))
Output
    0.3000    0.6000    0.1000
    1.0000

Notes

When only a number of bins is given, MatLivre may place the rounded edges differently from MATLAB. With explicit edges, widths or limits the counts match.

See also

histcounts histogram2 hist morebins fewerbins bar