Same bins for histogram
조회 수: 52 (최근 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에서 Line Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!