binscatter

Shows the density of scattered points by counting them in rectangular bins.

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

Syntax

binscatter(x, y)

Bins the points (x, y) and colors each bin by its count.

binscatter(x, y, N)

Uses N bins in each direction, or [Nx Ny].

binscatter(___, Name, Value)

Sets options such as 'NumBins'.

h = binscatter(___)

Also returns the Binscatter object.

Description

A scatter plot hides structure when thousands of points overlap. binscatter divides the plane into a grid and colors each cell by how many points it holds, so dense regions stand out. Add colorbar to read the counts.

Input arguments

x, y
Vectors of the same length with the point coordinates.
'NumBins'
Number of bins as [Nx Ny].

Output arguments

h
The Binscatter object; its NumBins property reports the grid used.

Example

Density of random points

rng(1)
x = randn(5000, 1);
y = x + 0.5*randn(5000, 1);
h = binscatter(x, y, [20 20]);
colorbar
disp(h.NumBins)
Output
    20    20

Notes

When the number of bins is not given, the automatic choice is an approximation and may differ from MATLAB's. Pass the number of bins to get the same grid.

See also

scatter histogram2 colorbar plot