Dividing part of a histogram

조회 수: 11 (최근 30일)
davut
davut 2023년 7월 14일
답변: Image Analyst 2023년 7월 14일
I want to plot a histogram but with divided parts. For example; it has four colums of age: 0-4, 4-8, 8-12 and 12-16. I want to divide 0-4 column into 5 parts, 4-8 into 10 parts. Is it possible?
  댓글 수: 5
davut
davut 2023년 7월 14일
like this ( I did it this manually)
Dyuman Joshi
Dyuman Joshi 2023년 7월 14일
It's not clear to me what your data is or what you are plotting.
Is your data the numbers in circles?

댓글을 달려면 로그인하십시오.

답변 (4개)

Florian Bidaud
Florian Bidaud 2023년 7월 14일
편집: Florian Bidaud 2023년 7월 14일
Let's say your data is stored like :
y = [2 8 5 10]
Where y(1) is your 0-4 range, y(2) your 5-8 range etc.
Then you could do something like
y_new = [];
for i = 1:2
y_new = [y_new repmat(y(i),1,5*i)];
end
y_new = [y_new y_new(3) y_new(4)]
and then use y_new to plot your histogram
However, from what I understood from your graph, your new histogram wouldn't represent the same thing as before, as each colomn would be equal to the total of the range.

Image Analyst
Image Analyst 2023년 7월 14일
Specify the "edges" you want for the bins as an input to histogram or histcounts
  댓글 수: 1
Steven Lord
Steven Lord 2023년 7월 14일
Or if you've already created a histogram, set its BinEdges property to the more refined bin edge vector or call the morebins or fewerbins functions with the histogram handle as input.
x = randn(1, 1e5);
h = histogram(x, -3:0.1:3); % bins with width of 0.1
figure % Making a second copy so you can compare original and modified
h = histogram(x, -3:0.1:3);
h.BinEdges = -3:0.25:3; % bins with width of 0.25
figure % third copy
h = histogram(x, -3:0.1:3);
fprintf("Before morebins call, histogram has %d bins.\n", h.NumBins)
Before morebins call, histogram has 60 bins.
morebins(h);
fprintf("After morebins call, histogram has %d bins.\n", h.NumBins)
After morebins call, histogram has 66 bins.
h.BinWidth % no longer width of 0.1
ans = 0.0909

댓글을 달려면 로그인하십시오.


davut
davut 2023년 7월 14일
no any useful answers unluckily
  댓글 수: 1
Dyuman Joshi
Dyuman Joshi 2023년 7월 14일
You have not answered my queries nor have you provided enough information for us to give a particular solution for your question.
@Image Analyst and @Steven Lord have given a general solution to the problem you have posted. If you want a "useful" solution, you will have specify "useful" details.

댓글을 달려면 로그인하십시오.


Image Analyst
Image Analyst 2023년 7월 14일
I think you may have overlooked my suggestion to use linspace to compute the edges, or you just coudn't figure it out. So here it is:
% For example; it has four colums of age: 0-4, 4-8, 8-12 and 12-16.
% I want to divide 0-4 column into 5 parts, and
% 4-8 into 10 parts. Is it possible?
edges1 = linspace(0, 4, 5+1);
edges2 = linspace(4, 8, 10+1);
edges = unique([edges1, edges2, 8, 12, 16])
edges = 1×18
0 0.8000 1.6000 2.4000 3.2000 4.0000 4.4000 4.8000 5.2000 5.6000 6.0000 6.4000 6.8000 7.2000 7.6000 8.0000 12.0000 16.0000
% Create sample data.
data = 5 + 2 * randn(1, 1000);
% Compute and plot histogram.
histogram(data, edges);
grid on;
ylabel('Count');
xlabel('Data Value');
Note that there are 5 bins between 0 and 4, 10 bins between 4 and 8, and then one bin from 8-12, and one bin from 12 to 16, just like you said you wanted.

카테고리

Help CenterFile Exchange에서 Data Distribution Plots에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by