Simple question regarding bar plot with categorical data
143 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
maho
am 6 Okt. 2017
Kommentiert: Henry
am 14 Okt. 2024
Hello guys,
I am new to matlab, and I'm trying to do a simple bar plot, like this:
x = ["bananas" "apples" "cherries"];
y = [14,12,7];
bar(categorical(x),y);
The problem is that bar() function seems to sort the x-labels in alphabetical order. Is there any way to override this behaviour, so that the x-labels are left in their original order?
0 Kommentare
Akzeptierte Antwort
Sean de Wolski
am 6 Okt. 2017
Bearbeitet: Sean de Wolski
am 6 Okt. 2017
x = categorical(["bananas" "apples" "cherries"]);
x = reordercats(x,{'bananas' 'apples' 'cherries'});
y = [14,12,7];
bar(x,y);
Categoricals can have order associated with them (for the purpose of relational operators).
7 Kommentare
Henry
am 14 Okt. 2024
Since you guys are fixing this. Could you also put in a ticket to fix this for the errorbar() plot
Weitere Antworten (4)
Steven Lord
am 6 Okt. 2017
If you're using release R2016b or later you could use histogram with a vector of BinCounts instead of using bar.
x = ["bananas" "apples" "cherries"];
C = categorical(x);
y = [14,12,7];
h = histogram('Categories', C, 'BinCounts', y);
0 Kommentare
Souarv De
am 8 Apr. 2021
Bearbeitet: Souarv De
am 8 Apr. 2021
I also faced the same issue. There are numerous shortcut techniques available to solve it out. What I used to follow is as below :
x = categorical(["bananas" "apples" "cherries"]);
x = reordercats(x,cellstr(x)');
y = [14,12,7];
bar(x,y);
0 Kommentare
Rik
am 6 Okt. 2017
Bearbeitet: Rik
am 6 Okt. 2017
The problem is not in bar, but in categorical. I can't find in the doc how to preserve order (with unique I know there is a switch to do so). So my suggestion would be to convert it yourself:
[~,ia,~]=unique(x,'stable');
x2=1:length(x);
x2=x(ia);
bar(x2,y)
xticks(x2)
xticklabels(x)%might not work, as this expects a cell stray containing strings
Of course, your labels are very likely to be unique, otherwise bar wil most likely yield an error, so this should work the same in all valid situations:
x2=1:length(x);
bar(x2,y)
xticks(x2)
xticklabels(x)%might not work, as this expects a cell stray containing strings
2 Kommentare
Rik
am 6 Okt. 2017
xticklabels and xticks were introduced in R2016b, so for earlier releases you should use gca and set the XTick and XTickLabels properties.
michael dupin
am 3 Okt. 2022
Bearbeitet: michael dupin
am 3 Okt. 2022
*** Use histogram and not bar :)
I've been struggling with this until now, so sharing with the community. You can use the histogram simply as a drawing function, not actually counting anything.
Including the vertical bars as well (instead of "barh"), as long labels are typically better displayed horizontally.
Et voila! Good luck. Mike
x = ["bananas" "apples" "cherries"];
y = [14,12,7];
histogram('Categories',x,'BinCounts',y,'orientation','horizontal');
histogram('Categories',x,'BinCounts',y);
0 Kommentare
Siehe auch
Kategorien
Mehr zu Axes Appearance 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!