histogram2

Bivariate histogram that counts pairs of values in a grid of bins and draws it as 3-D bars.

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

Syntax

histogram2(X, Y)

Bins the pairs (X(k), Y(k)) automatically and draws the counts.

histogram2(X, Y, nbins)

Uses nbins bins in each direction, or [nx ny].

histogram2(X, Y, Xedges, Yedges)

Uses the given bin edges in each direction.

histogram2(___, Name, Value)

Sets options such as 'Normalization', 'XBinEdges' or 'YBinEdges'.

h = histogram2(___)

Also returns the Histogram2 object.

Description

Each pair falls in one rectangular cell of the grid, and the height of the bar over a cell is the number of pairs in it. The bins are half-open like those of histogram, and pairs containing NaN are skipped.

Input arguments

X, Y
Data vectors of the same length.
Xedges, Yedges
Vectors of bin edges for each coordinate.

Output arguments

h
Histogram2 object. h.Values is the matrix of counts, with x bins down the rows and y bins across the columns.

Example

Counts on a grid

x = [1 2 2 3 4];
y = [1 1 2 3 4];
h = histogram2(x, y, [0 2 4 6], [0 2 4 6]);
h.Values
Output
ans = 3×3

     1     0     0
     1     2     0
     0     0     1

See also

histogram histcounts2 heatmap scatter