필터 지우기
필터 지우기

How to show partial legend in a loop

조회 수: 5 (최근 30일)
Ibrahs
Ibrahs 2022년 8월 22일
댓글: Ibrahs 2022년 8월 22일
Hi everyone,
I have to combine plot and area functions in a loop as below. I just want to show the legend for plot and not for area.
My code below show Graph 1:
f = figure;
hold on
for j = tt_n'
h1{j}=plot(Y, D{h}{j},'color',getprop(colors,j), 'linestyle',getprop(linestyle,j),'DisplayName', [num2str(year(Time{h}(j))+s1)]);
h2{j}=area(limx_tot_n{h}{j},limy_tot_n{h}{j},'FaceColor',getprop(colors,j));
end
grid on
legend('show','Location', 'NorthWest');
hold off
How can I delete the legend for the shaded area ?
When I modify the code as the following, I get Graph 2 below:
f = figure;
hold on
for j = tt_n'
h1{j}=plot(Y, D{h}{j},'color',getprop(colors,j), 'linestyle',getprop(linestyle,j));
labels_h1{j}=[num2str(year(Time{h}(j))+s1)];
h2{j}=area(limx_tot_n{h}{j},limy_tot_n{h}{j},'FaceColor',getprop(colors,j));
end
grid on
legend(labels_h1,'Location', 'NorthWest');
hold off
How can I solve this issue ?
Thank you in advance,

채택된 답변

Chunru
Chunru 2022년 8월 22일
편집: Chunru 2022년 8월 22일
f = figure;
hold on
for j = tt_n'
% h1{j}=plot(Y, D{h}{j},'color',getprop(colors,j), 'linestyle',getprop(linestyle,j));
h1(j)=plot(Y, D{h}{j},'color',getprop(colors,j), 'linestyle',getprop(linestyle,j));
labels_h1{j}=[num2str(year(Time{h}(j))+s1)];
h2{j}=area(limx_tot_n{h}{j},limy_tot_n{h}{j},'FaceColor',getprop(colors,j));
end
grid on
legend(h1, labels_h1,'Location', 'NorthWest'); % only include objects h1 in legend
hold off

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by