Borrar filtros
Borrar filtros

How to show partial legend in a loop

5 visualizaciones (últimos 30 días)
Ibrahs
Ibrahs el 22 de Ag. de 2022
Comentada: Ibrahs el 22 de Ag. de 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,

Respuesta aceptada

Chunru
Chunru el 22 de Ag. de 2022
Editada: Chunru el 22 de Ag. de 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

Más respuestas (0)

Community Treasure Hunt

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

Start Hunting!

Translated by