Azzera filtri
Azzera filtri

How to show partial legend in a loop

5 visualizzazioni (ultimi 30 giorni)
Ibrahs
Ibrahs il 22 Ago 2022
Commentato: Ibrahs il 22 Ago 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,

Risposta accettata

Chunru
Chunru il 22 Ago 2022
Modificato: Chunru il 22 Ago 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

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by