
How to change the color of a stacked bar?
4 ビュー (過去 30 日間)
古いコメントを表示
Hi all,
I have a stacked bar that has a legend but I don't like the way colors are represting it. I want the orange color be the first instead of blue, how can I modify it?
0 件のコメント
採用された回答
DGM
2021 年 3 月 27 日
編集済み: DGM
2021 年 3 月 27 日
There are probably various ways to solve this. If the colors are adequate and you merely want to change their ordering, consider the example:
clc; clf;
% simple stacked bar chart
A = [3 2 5; 2 1 6; 5 8 2];
subplot(1,2,1);
bar(A, 'stacked');
subplot(1,2,2);
h=bar(A, 'stacked');
newordering=[3 2 1];
CD={h.FaceColor};
for c=1:numel(h)
h(c).FaceColor=CD{newordering(c)};
end
This gives us the result:

In this case, I reversed the color order, but you can specify any order with the variable newordering.
This works in R2019b, but not R2015b. Unless specified otherwise, I'm going to assume you are using a relatively new version.
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Legend についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!