how to plot a bar graph using for loop?

15 ビュー (過去 30 日間)
Nma
Nma 2023 年 5 月 2 日
編集済み: dpb 2023 年 5 月 3 日
I have gotten my ouput results using a for loop. I have these results as :
Young aged positv test results: 7
Middle aged positive test results: 20
Old aged positive test results: 25
The x axis would be age groups, the y axis would be the # of positive results.
The code I used was here, but it displayed no bar graph:
%Read the data from positive test results
data.COVIDStatus(k)==1
%Include data which satisfies k condition for the appropriate age group
youngage_countx = 7
middleage_countx = 20
oldagecount_x = 25
%Create a bar graph which displays each x value
x = 7
x = 20
x = 25
%name y axis as positive test results

採用された回答

Image Analyst
Image Analyst 2023 年 5 月 2 日
but you haven't accepted it yet. If my Answer solved your original question, then could you please click the "Accept this answer" link to award the answerer with "reputation points" for their efforts in helping you? They'd appreciate it. Thanks in advance. 🙂 Note: you can only accept one answer (so pick the best one) but you can click the "Vote" icon for as many Answers as you want. Voting for an answer will also award reputation points.
After that, see this:
youngage_countx = 7;
middleage_countx = 20;
oldagecount_x = 25;
values = [youngage_countx, middleage_countx, oldagecount_x];
% Create a bar graph which displays each x value
xLabels = {'Children & Young', 'Middle Age', 'Old Age'};
cats = categorical(xLabels);
bar(cats, values);
% Name y axis as positive test results
ylabel('Positive Test Results')
xlabel('Age of Patient')
grid on;
  1 件のコメント
Nma
Nma 2023 年 5 月 2 日
Thanks so much! And I did accept your answer in my previous post, I'm not sure why it did not show up in your end. I do appreciate the help

サインインしてコメントする。

その他の回答 (1 件)

dpb
dpb 2023 年 5 月 2 日
編集済み: dpb 2023 年 5 月 3 日
%Read the data from positive test results
%data.COVIDStatus(k)==1
%Include data which satisfies k condition for the appropriate age group
youngage_countx = 7;
middleage_countx = 20;
oldagecount_x = 25;
%Create a bar graph which displays each x value
%name y axis as positive test results
x = [youngage_countx; middleage_countx;oldagecount_x];
ages=categorical({'Young','Middle','Old'},{'Young','Middle','Old'},'ordinal',1);
hB=bar(ages,x);
ylabel('Positive Counts')
title('Covid Positive Counts by Age Group')
Avoid creating multiple variables with long names containing the metadata of some characteristic within the name to distinguish them; use arrays or cell arrays and corollary variable(s) with which to identify them.
  2 件のコメント
Image Analyst
Image Analyst 2023 年 5 月 2 日
I tried ordinal=1 but without copying the category names in categorical and it sorted them alphabetically for some reason. That's why I had to add Children to my Young category, otherwise is, for some weird reason, sorted them and there was no option to not sort them that I could figure out.
dpb
dpb 2023 年 5 月 3 日
編集済み: dpb 2023 年 5 月 3 日
"I tried ordinal=1 but without copying the category names in categorical and it sorted them alphabetically for some reason."
Yeah, the categorical class isn't the most user-friendly thing, fer shure...specifying both the valueset and the catnames argument is the only way I could ever get 'ordinal' to actually have the desired effect. It'll say it's ordinal, and the tests of rank will work with the input order, but the display order always is alphabetical. I think it should qualify as a bug (or at least quality of implementation issue), but I don't recall whether I ever did submit a formal bug report or not.
If you provide the list in the desired order as the catnames second argument, that will convince it you meant what you said; it would seem to me that should be implied.
"That's why I had to add Children to my Young category, ...and there was no option to not sort them that I could figure out."
Only by actually using reordercats and redefining the variable will the the internal order be displayed externally--but it can be done.
c=categorical({'Fred','Barney','Wilma'},'ordinal',1); % create ordinal in nonalphabetic order
isordinal(c) % it is ordinal; it says so...
ans = logical
1
categories(c) % show names (sorted)
ans = 3×1 cell array
{'Barney'} {'Fred' } {'Wilma' }
c(c=='Wilma')>c(c=='Fred') % test that hypothesis
ans = logical
1
c(c=='Wilma')==c(c=='Fred') % test that hypothesis
ans = logical
0
c=reordercats(c,{'Fred','Barney','Wilma'}); % put 'em in order intended...
categories(c)
ans = 3×1 cell array
{'Fred' } {'Barney'} {'Wilma' }
Now have beat it into submission. Same thing happens on creation if use the other form...
c={'Fred','Barney','Wilma'};
c=categorical(c,c,'ordinal',1); % create ordinal in nonalphabetic order
categories(c)
ans = 3×1 cell array
{'Fred' } {'Barney'} {'Wilma' }

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCategorical Arrays についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by