Box plot with categorical axis - without LaTex interpreter
Afficher commentaires plus anciens
I have a box plot for the prices of 8 different varieties of apples, the 8 distinct varieties are contained in 'variety' column of S_apples
I would like to label the x-axis by those 8 varieties.
Here's my code:
T=readtable('fruitvegprices2.csv');
items={'apples','pears','carrots','cabbage'};
%apples
[idx,ia] = ismember(T.(2),items{1});
T_apples = T(idx,{'item','variety'});% this gives table of apples alone
S_apples=unique(T_apples,'stable') %distinct varieties of apples
A=sortrows(T(idx,{'variety','price'})) %grouping the varieties together
C=cell(length(S_apples.variety),1);%column i=prices of variety i
for i=1:length(S_apples.variety)
[c,d]=ismember(A.variety,S_apples.(2){i});
for k=1:length(A(c,:).price)
C{k,i}=A(c,:).price(k);
end
end
C(find(cellfun(@isempty,C)))={nan};
boxchart(cell2mat(C))
xlabel('Variety')
ylabel('Price')
I've tried creating categorical variables:
axis=categorical(cell2mat(C),1:8,S_apples.variety);
plot=boxchart(axis,cell2mat(C))
but it doesn't seem to work, it says: passing xgroup data isn't supported when ydata is a matrix.
How can I fix this?
Thank you very much!
2 commentaires
dpb
le 13 Déc 2021
I could make up an example, but it would be much simpler and more directly applicable to your case if you would attach the table T as a .mat file -- or the original input file.
Achuan Chen
le 13 Déc 2021
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Line Plots dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!