MATLAB Answers

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

16 views (last 30 days)
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 Comments

Show 7 older comments
Adam Danz on 7 Feb 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'})
Excellent alternative solution.
Thanks
(but the actual problem is unsolved)
Adam Danz on 7 Feb 2020
I understand. Walter pointed you to a function on the file exchange that might be helpful in building a custom legend.

Sign in to comment.

### Accepted Answer

Jeremy Marcyoniak on 7 Feb 2020
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.

#### 3 Comments

Adam Danz on 7 Feb 2020
+1; Good idea, Jeremy!
One small note of caution: since r2018b only the first output of the legend function is documented. Archived documentation for the legend function from r2016a to r2018a suggests avoiding the use of the outputs other than the first output since it "... creates a legend that does not support all graphics features".
For example, when the first and second outputs to legend() are both included, it interferes with the ability to change the fontsize of the legend text.
But it's simple and solves the problem. It might be wise for Lemuel to add a reminder-comment near this section of code in case there's weird problems in the future.
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 on 8 Feb 2020
Good to hear!

Sign in to comment.