How to set a legend with a label for two or more lines?
Mostrar comentarios más antiguos
Hello,
In the graphic legend I want to share the same label for two or more lines.
For example, I have two (or more) pair of data
x = linspace(0,0.5);
for i = 1:3
y1(i,:) = x.^2 + i;
y2(i,:) = (2*x).^2 + i;
end
My plot instructions are
hold on
plot(x,y1,'-')
set(gca,'ColorOrderIndex',1)
plot(x,y2,'--')
legend({'label 1','label 2','label 3'})
I want that the first curves of y1 and y2 share the label “label 1” and so on for the second and the thirds lines, “label 2” and “label 3”, respectively. Something like this


Thanks in advance for the answers.
10 comentarios
Walter Roberson
el 7 de Feb. de 2020
Same technique as I happened to answer earlier today in
Lemuel Carlos Ramos Arzola
el 7 de Feb. de 2020
Walter Roberson
el 7 de Feb. de 2020
Ah. In that case, you will need to construct your own legend box. legend() is not designed to be able to have more than one linestyle (or color) for any given label.
Lemuel Carlos Ramos Arzola
el 7 de Feb. de 2020
Walter Roberson
el 7 de Feb. de 2020
https://www.mathworks.com/matlabcentral/fileexchange/31092-legendflex-m-a-more-flexible-customizable-legend makes a good starting point.
If the solid and dashed lines represent the same thing within each color, why not just show the solid lines in the legend?
hold on
h = plot(x,y1,'-');
set(gca,'ColorOrderIndex',1)
plot(x,y2,'--')
legend(h, {'label 1','label 2','label 3'})
If the dashed lines must be included in the legend, IMO having duplicate labels would be easier than construction your own legend.
Lemuel Carlos Ramos Arzola
el 7 de Feb. de 2020
Editada: Lemuel Carlos Ramos Arzola
el 7 de Feb. de 2020
Matlab needs to have an option for grouped symbols/linestyles/colors in the legend. But it currently doesn't. In these cases I've done something like this. It defines the colors and line styles as groups.
h = plot(x,y1,'-');
set(gca,'ColorOrderIndex',1)
plot(x,y2,'--')
h2 = plot(nan, nan, 'k-');
h3 = plot(nan, nan, 'k--');
legend([h;h2;h3], {'label 1','label 2','label 3', 'Newtonian',' Non-Newtonian'})

Lemuel Carlos Ramos Arzola
el 7 de Feb. de 2020
Adam Danz
el 7 de Feb. de 2020
I understand. Walter pointed you to a function on the file exchange that might be helpful in building a custom legend.
Respuesta aceptada
Más respuestas (1)
Laurens
el 31 de Oct. de 2024
0 votos
I understood the problem as follows: One legend entry can correspond to many lines on the graph. But Matlab assigns the first legend entry to colors/markers of the first line on the graph, the second entrie's colors/markers correspond to the second line in the graph and so on.
My solution therefore was to tell Matlab exactly - for each group of lines - which line belongs to which entry. you only have to do this once for each legend entry.
Exemple code:
figure
p1 = plot(x, y3, 'LineWidth', 0.3, 'Color', [0.4, 0.4, 0.4, 0.05]);
hold on
p2 = plot(x, y1, 'LineWidth', 2, 'Color', [1, 0, 0, 0.1]);
hold on
p3 = plot(x, y2, 'LineWidth', 5, 'Color', [0.9, 0, 0, 1]);
legend([p1(1,:); p2(1,:); p3(1,:)], {'entry 1','entry 2', 'entry 3' }, "Location","southoutside")
p1(1,:) samples one line (the first) from the first group of lines
p2(1,:) samples one line from the second group of lines
and so on ...
Categorías
Más información sobre Image Arithmetic en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

