How to plot a 2D histogram using matlab
Posted on August 17, 2006
So, I wanted to plot a 2D histogram in matlab, but the pickings were slim. I really just wanted something that worked as much like hist as possible. So I wrote my own:
HIST2D calculates a 2-dimensional histogram
N = HIST2D(X,Y) bins the X and Y data into 10 equally spaced bins in both dimensions
N = HIST2D(X,Y,M), where M is a scalar, bins the data into M equally spaced bins in both dimensions
N = HIST2D(X,Y,B), where B is a vector, bins the data with centers specified by B
The number of bins or centers can be specified individually for either dimension using N = HIST2D(X,Y,NX,NY) or N = HIST2D(X,Y,BX,BY)
[N,BX,BY] = HIST2D(…) also returns the positions of the bin centers as two matrices in BX and BY
HIST2D(…) without output arguments produces a colormapped image plot of the 2d histogram
EXAMPLE
yin = randn(1,1000); xin = randn(1,1000); [n,x,y] = hist2d(xin,yin,11); imagesc(x(1,:),y(:,1),n); hold on; plot(xin,yin,'y.'); colorbar
You can download it here: hist2d.m
Comments
15 Responses to “How to plot a 2D histogram using matlab”
Leave a Reply
thanks
actually there’s a bug in your example; the image is upside-down. To fix this, replace
imagesc(x(1,:),y(:,1),n);
with
imagesc(x(1,:),-y(:,1),n);
I hope this helps!
Regards,
Danny
P.S. to check this, I ran
[n,x,y] = hist2d(-100:100, -100:100,80) ; imagesc(x(1,:),-y(:,1),n);
which represents the function y(x) = x, clearly the slope should be positive one (not negative one).
Regards,
Danny
Thanks you very much
thanks david
why don’t you submit this in matlab central?
indeed its great that your code is a close to matlab’s hist as possible
chris
David,
Thank you VERY much for contributing to the greater good. Thanks Danny for finding the bug.
-Jeremy
Respect!
Thanks, works great!
Hi
how can i plot and calculate the histogram of the image
Hi,
I dont think that there was a mistake before, just that image does things upside down from what one might expect.
So before it plotted the correct thing but flipped upside down. Now it plots the correct thing but with the y-axis the wrong way. I can’t seem to find a simple way to fix this as the axis seems to like going from min at the top to max at the bottom.
But thanks alot this is still a very useful routine.
Dan
Actually you can make it plot things the correct way with
axis xy;
Just put this after the imagesc(.) line.
Dan
Thanks very much indeed. With use of axis tick settings and grid showing I could get a log-log 2-D histogram to work fine also.
to fix the problem with Y-axis, just set the direction:
>> set(gca,’ydir’,'normal’);
should be the same results of
>> axis xy;
As point Dan.
Looks like a working solution, thanx for this…let us spread the word on our site too…
CroAxis
[...] you be interested in this topic then you need to follow this MATLAB link here. You can find the working solution there along with a link to download… Share and [...]
Thanks.but i need code for histrogram of finger image.please help me