Borrar filtros
Borrar filtros

Legend for selective plot

1 visualización (últimos 30 días)
klb
klb el 26 de Abr. de 2023
Comentada: klb el 27 de Abr. de 2023
Hello everyone,
I want legend to automatically update based on what was selcted to plot. I want only vectors that are plotted to be indicated within the legend. How do i do that help!
%first column is the counter ID number, and remaining vector are values the counter counted.
my_mat = [11 2 4 5 6;
14 4 5 6 7;
25 2 3 4 5;
56 0 9 8 6]
my_mat = 4×5
11 2 4 5 6 14 4 5 6 7 25 2 3 4 5 56 0 9 8 6
counts = size(my_mat,1);
% Plot only if the the counter ID is amongst the values specified in the if-elseif-else criterion.
for u = 1: size(my_mat,1)
d = my_mat(u);
hold on
if d == 11
plot(1:counts, my_mat(u, 2:end),"LineWidth",1,"Color",'r', "LineStyle","-")
elseif d == 14
plot(1:counts, my_mat(u, 2:end),"LineWidth",1,"Color",'r', "LineStyle","--")
elseif d == 34 % not an error. Counter 25 not in criteria, therefore wont be plotted
plot(1:counts, my_mat(u, 2:end),"LineWidth",1,"Color",'b', "LineStyle","-")
elseif d == 56
plot(1:counts, my_mat(u, 2:end),"LineWidth",1,"Color",'b', "LineStyle","--")
end
end
legend('11','14','60','56', "Location","southeast") % As seen , the legend is not 'linked' to what is actually plotted. Its hardwired therefore errorprone as shown. I want only vectors that are actually plotted to be indicated within the legend. help!
Warning: Ignoring extra legend entries.
Thank you for your time.

Respuesta aceptada

Cris LaPierre
Cris LaPierre el 26 de Abr. de 2023
%first column is the counter ID number, and remaining vector are values the counter counted.
my_mat = [11 2 4 5 6;
14 4 5 6 7;
25 2 3 4 5;
56 0 9 8 6];
counts = size(my_mat,1);
% Plot only if the the counter ID is amongst the values specified in the if-elseif-else criterion.
for u = 1: size(my_mat,1)
d = my_mat(u);
hold on
if d == 11
plot(1:counts, my_mat(u, 2:end),"LineWidth",1,"Color",'r', "LineStyle","-",'DisplayName','11')
elseif d == 14
plot(1:counts, my_mat(u, 2:end),"LineWidth",1,"Color",'r', "LineStyle","--",'DisplayName','14')
elseif d == 34 % not an error. Counter 25 not in criteria, therefore wont be plotted
plot(1:counts, my_mat(u, 2:end),"LineWidth",1,"Color",'b', "LineStyle","-",'DisplayName','34')
elseif d == 56
plot(1:counts, my_mat(u, 2:end),"LineWidth",1,"Color",'b', "LineStyle","--",'DisplayName','56')
end
end
legend("Location","southeast")
  1 comentario
klb
klb el 27 de Abr. de 2023
That works. Thank you!

Iniciar sesión para comentar.

Más respuestas (0)

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by