Same bins for histogram
63 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(2 个)
Rik
2019-1-7
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 个评论
Benoit Espinola
2020-6-15
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
2020-6-15
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
2019-1-14
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Histograms 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!