Simple question regarding bar plot with categorical data

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?

 채택된 답변

Sean de Wolski
Sean de Wolski 2017년 10월 6일
편집: Sean de Wolski 2017년 10월 6일
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

This works as expected. Thanks Sean.
I am using R2017a and experiencing a similar issue with the sort order of categorical labels in bar charts. However, my issue does appear to be in the bar and not with categorical.
list = {'t1','t2','t3','t4','t5','t6','t7','t8','t9','t10','t11'};
c = categorical(list);
The order of the array matches the order of list. However, when using the following, the categorical axis labels are reordered along with the values in NumVector, such that their new order matches list2 below and is not desired.
bar(c,NumVector)
list2 = {'t1','t10','t11','t2','t3','t4','t5','t6','t7','t8','t9'};
Since the NumVector input values have also been reordered, it is not simply possible to replace the XTickLabel with the values in list as this results in mismatched labeling. Any suggestions on how to get around this issue?
Allen, ,y default it's sorting the categories. You can pass in the valueset yourself which will enforce order or call reorder like I did above.
list = {'t1','t2','t3','t4','t5','t6','t7','t8','t9','t10','t11'};
c = categorical(list,list);
bar(c,1:numel(c))
Thanks!
Even better with coversion to string:
x = categorical(["bananas" "apples" "cherries"]);
x = reordercats(x,string(x));
y = [14,12,7];
bar(x,y);
Starting in R2023b, you can specify x as a string vector (or a cell array of character vectors), and MATLAB preserves the order. For example:
x = ["bananas" "apples" "cherries"];
y = [14,12,7];
bar(x,y)
Since you guys are fixing this. Could you also put in a ticket to fix this for the errorbar() plot

댓글을 달려면 로그인하십시오.

추가 답변 (4개)

Steven Lord
Steven Lord 2017년 10월 6일
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);
Souarv De
Souarv De 2021년 4월 8일
편집: Souarv De 2021년 4월 8일
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);
Rik
Rik 2017년 10월 6일
편집: Rik 2017년 10월 6일
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

xticklabels and xticks were introduced in R2016b, so for earlier releases you should use gca and set the XTick and XTickLabels properties.
Thank you for your response, Rik!
Your solution works, but it doesn't produce the same graphical output as using bar() with a categorical 'x' variable. In particular, if you squash the graph window horizontally, so that it is tall and narrow, you see that:
bar() using categorical variables very cleverly rotates the labels so they don't mush together
bar() using xticklabels() not so cleverly mushes the labels together so they become unreadable. :(
The same thing happens if you have many columns in your bar chart in relation to the physical width of your graph.

댓글을 달려면 로그인하십시오.

michael dupin
michael dupin 2022년 10월 3일
편집: michael dupin 2022년 10월 3일
*** 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);

카테고리

도움말 센터File Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기

질문:

2017년 10월 6일

댓글:

2024년 10월 14일

Community Treasure Hunt

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

Start Hunting!

Translated by