how to make intervals in a bar graph

조회 수: 63 (최근 30일)
Sadiq Akbar
Sadiq Akbar 2020년 10월 21일
댓글: Sadiq Akbar 2020년 10월 23일
Suppose I have some people and they are of different weight, e.g.
weight in Kq: 40-45 45-50 50-55 55-60 60-65
No. of Persons: 4 12 13 6 5
I want to represent the weight on x-axis and No. of persons on y-axis. This will tell us that there are 4 persons whose weight lies within 40-45 and 12 persons whose weight lies in the interval 45-50 kilo and so on. How can we do that?

답변 (2개)

Adam Danz
Adam Danz 2020년 10월 21일
편집: Adam Danz 2020년 10월 21일
For continuous intervals
Since your intervals are continuous, you should be using histogram() instead of bar().
kg = 40:5:65;
n = [4 12 13 6 5];
histogram('BinEdges',kg,'BinCounts',n)
xlabel('Weight (kg)')
ylabel('Number of participants')
For discountinuous or categorical intervals
For any future visitors who are not using continuous x-bins and want to label bin edges, follow this demo.
% Create data
bins = [10 20;
50 60;
75 85];
y = [20;10;30];
% Create bar plot
% The .5 specifies bin width, making room for labels
h = bar(y, .5);
% Get bar centers and bar widths
xCnt = h.XData + h.XOffset; % XOffset is undocumented!
width = h.BarWidth;
% Get x-val of bar-edges
barEdgesX = xCnt + width.*[-.5;.5];
% Set new xtick and xticklabels, rotate by 90deg.
ax = h.Parent; % axis handle, if you don't have it already
ax.XTick = barEdgesX(:);
ax.XTickLabel = string(reshape(bins',[],1));
ax.XTickLabelRotation = 90;
  댓글 수: 2
the cyclist
the cyclist 2020년 10월 21일
For "discontinuous" bins, it might be simpler to use your original syntax, but with a zero bin count for empty bins.
For example,
kg = 40:5:75;
n = [4 0 12 13 0 6 5];
figure
histogram('BinEdges',kg,'BinCounts',n)
xlabel('Weight (kg)')
ylabel('Number of participants')
Adam Danz
Adam Danz 2020년 10월 21일
Oh, that's interesting!
Thanks for the idea, the cyclist.
I just tested it with NaNs as spacers too but histogram throws an error that N must be finite.

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


the cyclist
the cyclist 2020년 10월 21일
편집: the cyclist 2020년 10월 21일
Here is one way:
w = 42.5 : 5.0 : 62.5;
n = [4 12 13 6 5];
bar(w,n,'BarWidth',1)
FYI, if you have the underlying individual weight data, rather than the bin counts, you will definitely want to use histogram as in Adam's solution (although the syntax will be different).
  댓글 수: 23
Sadiq Akbar
Sadiq Akbar 2020년 10월 22일
Thank you very much dear Adam Danz.Yes you are right that my 1st data of Kg was different than this one. Its becuase I posted this data with the question that what will be the histogram/bar plot for this, but no one did an attempt on that. Then I thought may be I am not able to make some one understand my problem. So I changed my question in the type which is understandable to all and therefore, I posted here as KG and n. And you and the cyclist attempted it.
Sadiq Akbar
Sadiq Akbar 2020년 10월 23일
I found one such code on this site today. The code is as follows:
[~,edges] = histcounts(log10(x));
histogram(x,10.^edges)
set(gca, 'xscale','log')
In this code, when I replace x by my data, then it plots well. Now I want to insert a preview in this graph. How can I insert the same graph as a preview pane in the same figure window as is in the attachment figure.

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by