How can I change bar graph x-axis?

Hello, I want to order my bar graph (left hand side) in descending order but didn't succeed in ordering x-axis labels (right hand side). Values in x-axis are feature indices. How can I fix it? Thanks for the help.
[a,b] = sort(Scores_Avg,2,'descend')
bar(Scores_Avg(b))
text(1:length(a), a', num2str(a','%0.3f'),...
'HorizontalAlignment','center','VerticalAlignment','bottom');
set(gca,'XTick',b);

 Accepted Answer

dpb
dpb on 11 Aug 2021
Edited: dpb on 11 Aug 2021
[a,ia] = sort(Scores_Avg,2,'descend'); % use more meaningful index variable
bar(a) % plot the sorted array vs linear order
xticks(1:numel(a)) % set the tick marks to match bar number
xticklabels(ia) % label x ticks to go with sorted order
...
You need to first also set xticks to the full number of bins if there are enough bars that the default tick marks don't put that many tick marks on the axes.
NB: the caveat in the comment below -- as N grows, space starts to get very dear...

6 Comments

MB
MB on 11 Aug 2021
Edited: MB on 12 Aug 2021
I have to see all feature indices ordered in x-axis. I have datasets with 60 features. I don't want to mess the values. For this dataset x-axis value of the first bar must be 14, 2nd bar must be 8 and so on. Here, I can't see all of them and 14 is out of scale.
See the incorporation of hint...although for 60 bars you'll have to make the font size quite small or stretch the figure size quite a lot in the x direction to have sufficient room to be able to read the tick labels -- one simply begins to run out of room...
MB
MB on 11 Aug 2021
Edited: MB on 12 Aug 2021
I am ranking features using "rankfeatures()" function. Each bar indicates the importance of the corresponding feature. That's why I want to see all indices below each bar. At first, I can see all feature indices using the following command:
set(gca,'XTick',1:length(Scores_Avg));
However, after sorting the "Scores_Avg", this command became useless. Btw, my display has 5K resolution, I not having any problem. Anyway I got what you mean, thanks for the help.
dpb
dpb on 11 Aug 2021
Edited: dpb on 11 Aug 2021
You still have to plot against and set the ticks to 1:N because that's what you've asked for in the call to bar() with the y vector only.
Then xticklabels puts the ordered bins on the ticks based on the sorted order.
If you want exact example, attach a datafile as a .mat file...otherwise an example I used here is
y=2*abs(rand(60,1))-(1-exp(0.01*[1:numel(y)].')); % starting random 60 bins
[ys,iy]=sort(y,'descend'); % sort decreasing
bar(ys)
xticks(1:numel(y))
xticklabels(iy)
hAx=gca;
hAx.XTickLabelRotation=90;
And confirm is what is wanted by
>> [ys(1) iy(1);ys(2) iy(2);ys(end) iy(end)]
ans =
2.7783 58.0000
2.5427 54.0000
0.3605 8.0000
>>
whch by inspection matches the first two and last plotted bins.
Thanks.
No problem; realized later shoulda' just done it that way originally because you can't count on the default ticks. Bad implementation choice internally, but just one of the many warts in bar().

Sign in to comment.

More Answers (1)

set(gca,'XtickLabels',arrayfun(@num2str,b,'UniformOutput',false));

3 Comments

Not working :(
what exactly is not working?
Henry thanks for the help much appreciated.

Sign in to comment.

Products

Release

R2021a

Asked:

MB
on 11 Aug 2021

Commented:

dpb
on 12 Aug 2021

Community Treasure Hunt

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

Start Hunting!