How to sort data in a bar plot based on its values on Y-axis?

24 ビュー (過去 30 日間)
Chutiphon Moranon
Chutiphon Moranon 2023 年 2 月 26 日
コメント済み: Star Strider 2023 年 3 月 1 日
Hi there,
I am working on a bar graph as shown below.
I would like to sort them 'descending' so that LS713A (the highest) is plotted at the left and then CH12B, CH202A, SC762B and SC748A (the lowest) respectively.
I tried to use reordercats but it does not work. Could you please give some example(s)?
Thank you and best regards.

採用された回答

Star Strider
Star Strider 2023 年 2 月 26 日
I assume the x-data are categorical, because otherwise this is straightforward.
Perhaps this —
x = 1:5;
y = rand(1,5);
xlbl = categorical(["A","B","C","D","E"]);
figure
bar(xlbl,y)
title('Original')
Ax = gca;
% get(Ax)
xv = Ax.Children.XData;
yv = Ax.Children.YData;
[~,idx] = sort(yv, 'descend');
xv = reordercats(xv, idx);
figure
bar(xv(idx), yv(idx))
title('Sorted')
.
  2 件のコメント
Chutiphon Moranon
Chutiphon Moranon 2023 年 3 月 1 日
Hi Star Strider. Thank you for your reply. Much appreicate.
Star Strider
Star Strider 2023 年 3 月 1 日
As always, my pleasure!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeShifting and Sorting Matrices についてさらに検索

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by