How to align multiline label and legend?
13 views (last 30 days)
Show older comments
Hello
Using sprintf it is possible to create multiline labels in a legend. However, as shown in the example below, it would be better if the legend symbol (the colored line) would always be aligned with the first line of the multiline label, so aligned on top instead of being centered. The current output looks a bit confusing. Is it possible to control the position of the legend symbol w.r.t. the label?
Thanks in advance.
x=1:5;
y1=x;
y2=x.^2;
y3=x.^3;
plot(x,y1)
hold on
plot(x,y2)
plot(x,y3)
legend({'first graph $y=x$', sprintf('%s\n%s', 'second graph', '$y=x^2$'), sprintf('%s\n%s', 'third graph', '$y=x^3$')}, 'Interpreter', 'latex')
0 Comments
Accepted Answer
dpb
on 2 Apr 2025
Edited: dpb
on 3 Apr 2025
I had no luck even "handle diving" with <Yair's undocumented tool> to try to get access to the lines and/or legend text properties internals with the current legend syntax.
But, if you revert to the old syntax that still has the internal axes it's based on, you can get the handles to the actual legend lines objects and move them at will. Of course, doing this breaks a lot of the newer features like the title, multiple columns and auto-updating content, etc., ...
x=[1:5].'; y=x.^[1:3];
subplot(2,1,1)
plot(x,y)
legend({'first graph $y=x$', sprintf('%s\n%s', 'second graph', '$y=x^2$'), sprintf('%s\n%s', 'third graph', '$y=x^3$')}, 'Interpreter', 'latex','location','northwest')
subplot(2,1,2)
plot(x,y)
[~,hIcons]=legend({'first graph $y=x$', sprintf('%s\n%s', 'second graph', '$y=x^2$'), sprintf('%s\n%s', 'third graph', '$y=x^3$')}, 'Interpreter', 'latex','location','northwest')
hIcons(6).YData=hIcons(6).YData+0.1;
hIcons(8).YData=hIcons(8).YData+0.1;
The warning is troublesome; that doesn't yet show in my R2021b locally, but until Mathworks comes up with solutions for such things, it's a real kick in the teeth.
There may be some trapdoor to get to these line objects if poke at the present legend object sufficiently, but I couldn't find them easily.
ADDENDUM
I didn't think of it at the time and haven't looked to see but should double-check the position of the text objects -- it may be their vertical position is just what you need, not the empirical move made above...
Well, it's not much code, let's look and see...
figure
plot(x,y)
[~,hIcons]=legend({'first graph $y=x$', sprintf('%s\n%s', 'second graph', '$y=x^2$'), sprintf('%s\n%s', 'third graph', '$y=x^3$')}, 'Interpreter', 'latex','location','northwest')
hIcons(2).VerticalAlignment='top';
hIcons(3).VerticalAlignment='top';
is too much as the y postions are the same. Looks like adjusting the line position is the better option, unfortunately.
ADDENDUM
I believe the lack of access to adjust the legend lines/text is a significant defect in newer version and should be subject of formal enhancement/bug report.
2 Comments
dpb
on 3 Apr 2025
I did poke at the current legend objet returned handle with undocumented some more this morning and concluded the necessary properties are not only not made visible but are actually not exposed at all... :(
More Answers (0)
See Also
Categories
Find more on Graphics Object Properties in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!