フィルターのクリア

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
  1 件のコメント
Ibrahs
Ibrahs 2022 年 8 月 22 日
Many thanks! It works well.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLegend についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by