How to set a legend with a label for two or more lines?

조회 수: 164 (최근 30일)
Lemuel Carlos Ramos Arzola
Lemuel Carlos Ramos Arzola 2020년 2월 7일
답변: Laurens 2024년 10월 31일
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
figure2.png
Thanks in advance for the answers.
  댓글 수: 10
Lemuel Carlos Ramos Arzola
Lemuel Carlos Ramos Arzola 2020년 2월 7일
Excellent alternative solution.
Thanks
(but the actual problem is unsolved)
Adam Danz
Adam Danz 2020년 2월 7일
I understand. Walter pointed you to a function on the file exchange that might be helpful in building a custom legend.

댓글을 달려면 로그인하십시오.

채택된 답변

Jeremy
Jeremy 2020년 2월 7일
This isn't a particularly elegant solution, but I think it does what you want.
x = linspace(0,0.5);
for i = 1:3
y1(i,:) = x.^2 + i;
y2(i,:) = (2*x).^2 + i;
end
hold on
plot(x,y1,'-')
set(gca,'ColorOrderIndex',1)
plot(x,y2,'--')
[lh, labelhandles] = legend({'label 1','label 2','label 3'});
labelhandles(5).LineStyle = '--';
labelhandles(4).YData = [0.83 0.83];
labelhandles(5).XData = [0.0460 0.5057]; labelhandles(5).YData = [0.760 0.760];
This will only update one of the legend entries, I trust you can extend this solution to the others. You could also choose a labelhandles variable with a shorter name to make it look a little neater, if you wanted.
Your YData property can be tweaked until you are happy with the appearance.
plot_legend.png
  댓글 수: 3
Lemuel Carlos Ramos Arzola
Lemuel Carlos Ramos Arzola 2020년 2월 7일
편집: Lemuel Carlos Ramos Arzola 2020년 2월 7일
Very good idea Jeremy!
A little bit complex but it tackled the problem. I will be working with the legend function outputs to generalize your idea in my actual code.
I hope in the future Matlab automates this issue.
Thanks a lot.
Dear Adam thanks for the "note of caution", very important too.
I love Matlab and now I also love the community.
Best,
Lemuel
Adam Danz
Adam Danz 2020년 2월 8일
Good to hear!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Laurens
Laurens 2024년 10월 31일
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 ...

카테고리

Help CenterFile Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by