how to plot a bar graph using for loop?
Mostra commenti meno recenti
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
Risposta accettata
Più risposte (1)
%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 Commenti
Image Analyst
il 2 Mag 2023
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.
"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...
categories(c) % show names (sorted)
c(c=='Wilma')>c(c=='Fred') % test that hypothesis
c(c=='Wilma')==c(c=='Fred') % test that hypothesis
c=reordercats(c,{'Fred','Barney','Wilma'}); % put 'em in order intended...
categories(c)
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)
Categorie
Scopri di più su Categorical Arrays in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

