How to sort data in a bar plot based on its values on Y-axis?
16 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
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')
.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Migrate GUIDE Apps 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!