Histogram Overlay with Controlled Bins

4 views (last 30 days)
Hi all, I need some help making as histogram. I have two data sets of 51 by 101. I would like to create a histogram similar to what I could make in excel. I would like to control the bin values themselves as opposed to the number of bins. More specifically, I want the following:
Bins: 0.00 0.25 0.50 0.75 1.00 1.25 1.50 1.75 2.00 2.25 2.50 2.75 3.00 3.25 3.50
Where bin 1 goes from 0.00-0.24, bin 2 goes 0.25-50, etc.
I would also like to know how many values from my datasets fit into each bin. Finally, I would like to plot an overlay of these two histograms so I can compare both datasets. However, if two bars are overlayed, I'd like to be able to see the bar behind also. Thank you very much for your help!

Accepted Answer

Image Analyst
Image Analyst on 12 Mar 2013
Use plot() instead of bar(). With plot(), it's easier to see two plots at the same time and easy to specify the color of each.
[counts1, values1] = hist(data(:), numberOfBins1);
[counts2, values2] = hist(data(:), numberOfBins2);
plot(values1, count1, 'r-');
hold on;
plot(values2, count2, 'b-');
  3 Comments
Image Analyst
Image Analyst on 15 Mar 2013
Edited: Image Analyst on 15 Mar 2013
I don't have your dataset so I can't test your code. True, the bins may be different - their range and centers - but plot() does not care about that. Bar() would, but plot() doesn't(). So when you said "this works but not fully" you were referring to YOUR code, not mine. Any reason why you didn't use mine, where I explicitly said to use plot() and to not use bar()? If it's because you want bars instead of a line plot, then you need to use histc() instead of hist() so you can specify fixed bin locations instead of just relying on whatever each hist() happens to pick.
David Polcari
David Polcari on 28 Mar 2013
I've updated my question so you can understand what I was talking about. I'm sorry about the bar() function. As you guessed, I wanted to have bars. Thanks for the advice.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!