How to change the color of a stacked bar?

조회 수: 18 (최근 30일)
Wolfgang McCormack
Wolfgang McCormack 2021년 3월 27일
댓글: Wolfgang McCormack 2021년 3월 27일
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?

채택된 답변

DGM
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.
  댓글 수: 1
Wolfgang McCormack
Wolfgang McCormack 2021년 3월 27일
@DGM Yes, I am actually using 2020. Thank you!

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

추가 답변 (0개)

카테고리

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

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by