Reorder x-axis bar graph categorical

19 Ansichten (letzte 30 Tage)
Mark Crook-Rumsey
Mark Crook-Rumsey am 27 Nov. 2019
Bearbeitet: Adam Danz am 20 Sep. 2023
Hi all,
I have viewed some of the other suggestions on here, but none have worked for me and I'm unsure where I am going wrong. I am trying to reorder a bar graph to be in the order I have specified in my variable cats.
errlow_ERP = [young_mid_parietal_PMconcept_stderr_ERP old_mid_parietal_PMconcept_stderr_ERP MCI_mid_parietal_PMconcept_stderr_ERP];
ERP_mean = [young_mid_parietal_PMconcept_mean_ERP old_mid_parietal_PMconcept_mean_ERP MCI_mid_parietal_PMconcept_mean_ERP];
errhigh_ERP = [young_mid_parietal_PMconcept_stderr_ERP old_mid_parietal_PMconcept_stderr_ERP MCI_mid_parietal_PMconcept_stderr_ERP];
cats = categorical({'Young','Older adult','MCI'});
subplot(3,4,12)
hold on;
p = bar(cats,ERP_mean,...
'FaceColor',[0.75,0.75,0.75],...
'EdgeColor','k',...
'LineWidth',1.5,...
'BaseValue',0);
baseline = p.BaseLine;
er = errorbar(cats,ERP_mean,errlow_ERP,errhigh_ERP,'.','vertical');
set(gca,'LineWidth',1,'FontSize',14)
ylim([0 8.5])
er.LineWidth = 1.5;
er.Color = 'k';
er.MarkerSize = 1;
xlabel('Group','FontSize',16)
ylabel('Amplitude in \muV','FontSize',16)
Any advice is greatly appreciated!

Akzeptierte Antwort

Adam Danz
Adam Danz am 27 Nov. 2019
Bearbeitet: Adam Danz am 20 Sep. 2023
Starting in MATLAB R2023b, bar accepts string arrays in addition to categoricals in the x-argument. The bar order will maintain the string order.
str = ["Young","Older adult","MCI"];
y = 1:3;
bar(str, y)
For releases prior to R2023b, you can reorder the categories using reordercats().
I know it doesn't look intuitive because the order specified by reordercats is the same as the original order but this sets that order in stone.
figure
cats = categorical({'Young','Older adult','MCI'});
cats = reordercats(cats,{'Young','Older adult','MCI'});
p = bar(cats, y);

Weitere Antworten (0)

Kategorien

Mehr zu Labels and Annotations finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by