필터 지우기
필터 지우기

Special legend for special bar plot

조회 수: 4 (최근 30일)
Daniel
Daniel 2021년 3월 10일
댓글: Star Strider 2021년 3월 11일
Here's the code for my bar plot:
ctgroup = reshape(Ct(2:end),5,[]);
ctgroup = [nan,Ct(1),nan;ctgroup];
figure, b = bar(1:6,ctgroup,'FaceColor','flat','HandleVisibility','off'); hold on, b(2).CData(1,:) = [0 0 0];
b(1).CData(2,:) = [0.4940 0.1840 0.5560]; b(1).CData(3,:) = [0.4940 0.1840 0.5560]; b(1).CData(4,:) = [0.4940 0.1840 0.5560];
b(1).CData(5,:) = [0.4940 0.1840 0.5560]; b(1).CData(6,:) = [0.4940 0.1840 0.5560];
b(2).CData(2,:) = [0 0.4470 0.7410]; b(2).CData(3,:) = [0 0.4470 0.7410]; b(2).CData(4,:) = [0 0.4470 0.7410];
b(2).CData(5,:) = [0 0.4470 0.7410]; b(2).CData(6,:) = [0 0.4470 0.7410];
b(3).CData(2,:) = [0.3010 0.7450 0.9330]; b(3).CData(3,:) = [0.3010 0.7450 0.9330]; b(3).CData(4,:) = [0.3010 0.7450 0.9330];
b(3).CData(5,:) = [0.3010 0.7450 0.9330]; b(3).CData(6,:) = [0.3010 0.7450 0.9330];
cases = {'Baseline','cst Pitch','cst TSR','max RR','min C_t','max C_t'};
set(gca,'xticklabels',cases); ax = gca; ax.XTick = 1:6;
ylabel('C_t','fontsize',14)
The plot is attached. I want to add a legend to this plot that shows the first four bar colors (that's each color once, or black, purple, dark blue, light blue), and nothing more. How can it be done?

채택된 답변

Star Strider
Star Strider 2021년 3월 11일
See if the approach in How to draw bar graph of different colors ? will do what you want.
  댓글 수: 5
Daniel
Daniel 2021년 3월 11일
편집: Daniel 2021년 3월 11일
Got it.
figure, hold on
for i = 1:length(ctgroup)
b{i} = bar(i,ctgroup(i,:),'FaceColor','flat');
end
b{1}(1).HandleVisibility = 'off';
b{1}(3).HandleVisibility = 'off';
for i = 1:length(b)
for j = 1:3
b{i}(j).FaceColor = barcols{i,j};
end
end
legend('Baseline','10% Derate','20% Derate','40% Derate')
Star Strider
Star Strider 2021년 3월 11일
I knew you could do it!
Thank you!

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

추가 답변 (0개)

카테고리

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

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by