## Efficient 2D histogram, no toolboxes needed

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

parameters

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.

'filt','filtering','smooth'

'log' : Change the colormap to be on a log scale to represent data

over a large dynamic range.

'logplot'

'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'

'f','numbins'

'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.

'themax'

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

'int'

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.

'normx','nx'

normalizey: Normalize the plot so that the sum of all the x values in each

y bin sum to one.

'normy','ny'

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.

'nr'

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

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

'threeD','3d','columns'

PARTIALLY IMPLEMENTED

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

NOT IMPLEMENTED YET

'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'

examples

To test the function you may use this example:

z=2*randn(1,100000)+1i*(randn(1,100000));

If you have amplitude and angle measures then pass this:

z = amp*exp(1i*ang);

NDHIST(z)

NDHIST(z,'lansey')

NDHIST(z,'filter')

% 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.

SEE ALSO: HIST, HIST3

### Cite As

Jonathan C. Lansey (2021). 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

##### Platform Compatibility

Windows macOS Linux##### Categories

##### Tags

##### Acknowledgements

**Inspired by:**
sanePColor(varargin), Plot and compare histograms; pretty by default, Beautiful and distinguishable line colors + colormap

**Inspired:**
2-D histogram plot

### Community Treasure Hunt

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

Start Hunting!### Discover Live Editor

Create scripts with code, output, and formatted text in a single executable document.