Efficient 2D histogram, no toolboxes needed

version (33.1 KB) by Jonathan C. Lansey
Creates 2D histogram of scatterplot or lineseries data, really fast, fully featured.


Updated Wed, 16 Sep 2015 15:59:51 +0000

View License

Displays a 2d histogram for your data, it will set the bins appropriately
NDHIST(x,y); where x and y are equal length vectors. It will choose
reasonable axis bounds and bins to present your data. The
default parameters may leave some data off the chart.
NDHIST(XY); where XY = [x y] and x,y are vectors
NDHIST(z); where z is a vector of complex numbers, (x+1i*y) or amplitude*exp(1i*theta)
NDHIST(y); where y is a vector of real numbers will plot a 2d histogram
of the points, as if it were a line-chart. It is equivalent
to calling ndhist(1:length(y),y);

N = NDHIST(x,y); returns a matrix N containing the counts for each bin
determined by the histogram.
[edgesX2,edgesY2,N] = NDHIST(x,y); which returns a matrix N containing
the counts for each bin determined by the histogram. You can
plot it with sanePColor(edgesX2,edgesY2,N); (from Matlabcentral)

NDHIST(...,'param','value','param','value', ... ); Run ndhist with specific

List of special parameters:

'filter' : This will apply a gaussian filter to the final histogram data.
The default filter width is 5 bins wide. If you pass a number
then that will be used. Even numbered filter parameters will be
changed to odd numbers to keep the filter perfectly symetrical.

'log' : Change the colormap to be on a log scale to represent data
over a large dynamic range.

'bins' : Change the size of the bins. For example '2' will create a
plot with twice the default number of bins; 0.5 will have half
the default number of bins. The default uses Scott's normal
reference rule. Unclear if it is ideal for 2d histograms...
If you are looking for a histogram with specific bins, use the
subfunction hist3. Feel free to implement it as an additional
parameter 'edgdes','edgesx' or 'edgesy'

'binsx' : Change the size of only the x bins. 'fx'
'binsy' : Change the size of only the y bins. 'fy'

axis : This is to set the range of the plot, [xmin xmax ymin ymax]
The default range is set to 3*std(x) and 3*std(y) where the
parameter stdTimes=3 is hard-coded in this version and
potentially added as a parameter in a later version.

max : This is to set the range of the plot to be such that every
point will be contained within the plot.

intbins : Set the bins to be intiger widths. For both x and y

intbinsx : Set the x bins to be intiger widths. 'intx'
intbinsy : Set the y bins to be intiger widths. 'inty'

normalizex: Normalize the plot so that the sum of all the y values in each
x bin sum to one.

normalizey: Normalize the plot so that the sum of all the x values in each
y bin sum to one.

normalizeR: Normalize the plot so that the you can clearly see how the
distribution vary's over angle. It weights points in the outer
radius by the diameter at that radius.

points: Plot the points on top of the colored histogram.

3D: Use a 3D column graph instead of a colored heatmap

radial : Set the axis to be equal and add a polar grid 'r'

'samebins': NOT IMPLEMENTED YET. Would set the width of the x and y bins
to be equal to each other and the axis equal too.

user parameters:
filter: This will filter the data, you may choose to follow it with a
number. This number will represent the radius of the circular
gaussian filter. Other ways to call it: 'filt','filtering','f'


To test the function you may use this example:

If you have amplitude and angle measures then pass this:
z = amp*exp(1i*ang);


% Note
The name of this function comes because really its a 2d hist, but since I
already have an 'nhist' I thought I might name it this.


Cite As

Jonathan C. Lansey (2022). Efficient 2D histogram, no toolboxes needed (https://www.mathworks.com/matlabcentral/fileexchange/45325-efficient-2d-histogram-no-toolboxes-needed), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2013b
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!