Same bins for histogram
63 views (last 30 days)
Show older comments
Hi,
I have this code - attached in this post.
When you run this program, it would give you several subplots with histogram in some of them.
The thing is, I want to make the bins the same for all the histograms. I put "10" but the widths are still different.
I have no idea how to fix is, I am very confused. I also attached one image, just in case.
I want the widths to be the same for columns 2 and 3.
Thanks a lot!
0 Comments
Answers (2)
Rik
on 7 Jan 2019
What you are telling Matlab is to divide the range [min(data) max(data)] in 10 equal parts. Because your data has differing ranges, these sizes are different as well.
You can fix this by calculating appropriate bins yourself, and use those as an argument in the calls to histogram.
3 Comments
Benoit Espinola
on 15 Jun 2020
I do not think this comment solved the question. I believe it is a step forward however the solution is not evident. It arises yet another question: how do you calculate the bins yourself?
An example code would be great.
Rik
on 15 Jun 2020
I believe it actually is evident once you take a look at the documentation. What exact bins are relevant will depend on your application. The documentation contains an example for how you can specify the bin edges. When you have decided what bin ranges are suited for your application you can use those same edges for every histogram. You may want to round min(data) and max(data) to determine those outer limits automatically, instead of hard-coding them as I did below.
% since the data in this example ranges from about -20 to 80:
edges=lispace(-20,80,20);
subplot(1,2,1)
histogram(data1,edges)
subplot(1,2,2)
histogram(data2,edges)
Image Analyst
on 14 Jan 2019
Instead of passing in one number, that represents the number of bins, pass in an array for the 'edges' option. This is how you can specify the edges, or dividing lines, of the bins. You'll still have the same number of bins, but the location of the bins will not move around based on the data like they would if you passed just the number of bins.
0 Comments
See Also
Categories
Find more on Histograms in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!