How can I change bar graph x-axis?

조회 수: 65 (최근 30일)
MByk
MByk 2021년 8월 11일
댓글: dpb 2021년 8월 12일
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);

채택된 답변

dpb
dpb 2021년 8월 11일
편집: dpb 2021년 8월 11일
[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
MByk
MByk 2021년 8월 12일
Thanks.
dpb
dpb 2021년 8월 12일
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().

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

추가 답변 (1개)

Henry Barth
Henry Barth 2021년 8월 11일
set(gca,'XtickLabels',arrayfun(@num2str,b,'UniformOutput',false));
  댓글 수: 3
Henry Barth
Henry Barth 2021년 8월 11일
what exactly is not working?
MByk
MByk 2021년 8월 12일
Henry thanks for the help much appreciated.

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

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by