39 views (last 30 days)

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?

Adam Danz
on 21 Oct 2020

Edited: Adam Danz
on 21 Oct 2020

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;

the cyclist
on 21 Oct 2020

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
on 21 Oct 2020

Oh, that's interesting!

I just tested it with NaNs as spacers too but histogram throws an error that N must be finite.

the cyclist
on 21 Oct 2020

Edited: the cyclist
on 21 Oct 2020

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).

Adam Danz
on 22 Oct 2020

If you plot the data from fitness2sn0.mat (variable 'one'), you'll see that is spans from x=0 to x=0.0022349. That's closer to the range of data in your histogram.jpg image (though it has a different distribution).

"Then why mine is not like that? or how can I get like that?"

I'm not sure if that's the right question to be asking.

The way I see it, the question isn't how to make your data look like that plot. The questions could be,

- (not really a question) Perhaps I am plotting it corretly and my data have a different result.
- Am I using at the right data in the first place? Should I be binning and counting this variable?
- I am using the correct data but the outcome is unexpected. Is that because of an error in the analysis, an error when importing the data, or is it a true reflection of whatever I was measuring?

We're at a crossroads here where no amount of technical help is going to answer these questions (without more background informat). You as the owner of the data and the person analyzing it needs to take a step back think about it from big-picture perspective.

I'll point out that the values in your original question (kilograms etc) don't seem to be relevant to the jpg image or the mat data you shared so I sense some disorientation here.

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

Start Hunting!
## 0 Comments

Sign in to comment.