Effacer les filtres
Effacer les filtres

How to show partial legend in a loop

5 vues (au cours des 30 derniers jours)
Ibrahs
Ibrahs le 22 Août 2022
Commenté : Ibrahs le 22 Août 2022
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,

Réponse acceptée

Chunru
Chunru le 22 Août 2022
Modifié(e) : Chunru le 22 Août 2022
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 commentaire
Ibrahs
Ibrahs le 22 Août 2022
Many thanks! It works well.

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by