File Exchange

image thumbnail

2-Dimensional Histogram

version (535 Bytes) by Rey Patlolla
Accumulate and plot a 2d histogram matrix.


Updated 06 Sep 2005

No License

function H = histo2D(D,[Xlo Xhi],Xn,[Ylo Yhi],Yn,Xlab,Ylab,Title)

2-Dimensional Histogram
(size(H) == [Yn Xn])
Counts number of points in the bins defined by -
X = linspace(Xlo,Xhi,Xn) and
Y = linspace(Ylo,Yhi,Yn)

Dx = 5*rand(100,1) ;
Dy = 10*rand(50,1) ;
D = [Dx,Dy] ;
H = histo2D(D,[0 5],10,[0 10],5, 'Xlabel','Ylabel','2d Histogram') ;

Cite As

Rey Patlolla (2021). 2-Dimensional Histogram (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (8)


worked well for me. As another user suggested, it does slow down quite a bit with larger data sets (or finer bin resolution).

Regarding the 'hist3' function, it requires the Statistics Toolbox, so this is a good option for users that don't have that toolbox.

Aaron Swan

MATLAB provides the function hist3 to generate bivariate histograms


tudor t

it's very slow, and it becomes a nuisance for large matrices -- it's almost an order of magnitude slower than the other hist2d, the one that uses histc; however, that one has a bug;

also, there's something funny with the plot, it seems it omits 1 row and 1 column...

anyway, it's easier to write than to debug so I wrote my own :-)

Ian Thorpe

Works very well and is easy to modify for your own needs. I had to transpose X and Y in the dsearchn calls to get it to work.

Ty Woo

Exactly what I was looking for. Had to fix the code only a little.

Paulo L

An example does not work.

Jascha Sohl-Dickstein

This is exactly what I was looking for. Thank you!

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

Inspired by: 2D Histogram Matrix

Inspired: 2D Histogram

Community Treasure Hunt

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

Start Hunting!