How to plot a 2D histogram using matlab

Posted on August 17, 2006

hist2dSo, 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

» Filed Under howto, matlab

Comments

16 Responses to “How to plot a 2D histogram using matlab”

  1. danny on December 14th, 2006 12:23

    thanks

  2. danny on December 15th, 2006 5:05

    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

  3. Giang on May 11th, 2007 3:18

    Thanks you very much

  4. christos on March 13th, 2008 0:17

    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

  5. Jeremy on August 29th, 2008 9:11

    David,

    Thank you VERY much for contributing to the greater good. Thanks Danny for finding the bug.

    -Jeremy

  6. Toni on December 19th, 2008 0:45

    Respect!

  7. Levi on January 13th, 2009 11:50

    Thanks, works great!

  8. Lifa on February 13th, 2009 22:12

    Hi
    how can i plot and calculate the histogram of the image

  9. Dan on February 28th, 2009 2:32

    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

  10. Dan on February 28th, 2009 2:36

    Actually you can make it plot things the correct way with

    axis xy;

    Just put this after the imagesc(.) line.

    Dan

  11. Gernot Hassenpflug on August 27th, 2009 10:47

    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.

  12. christian on November 13th, 2009 5:20

    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.

  13. Matlab on December 24th, 2009 5:32

    Looks like a working solution, thanx for this…let us spread the word on our site too…

    CroAxis

  14. 2D Histogram Plotting in MATLAB | CroAxis.com on December 24th, 2009 5:41

    [...] 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 [...]

  15. Sirajudeen on March 10th, 2010 12:37

    Thanks.but i need code for histrogram of finger image.please help me

  16. Frederik Lund on April 8th, 2010 6:06

    Thanks alot with the suggested fix for the direction of the y-axis it works great

  • Pages

  • Recent Posts

  • Categories

  • Interesting from Elsewhere

  • Meta