How to align multiline label and legend?

조회 수: 16 (최근 30일)
Stefan
Stefan 2025년 4월 2일
댓글: dpb 2025년 4월 3일
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')

채택된 답변

dpb
dpb 2025년 4월 2일
편집: dpb 2025년 4월 3일
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')
Warning: Calling legend with multiple outputs will not be supported in a future release.
hIcons =
9x1 graphics array: Text (first graph $y=x$) Text (second graph, $y=x^2$) Text (third graph, $y=x^3$) Line (first graph $y=x$) Line (first graph $y=x$) Line (second graph/$y=x^2$) Line (second graph/$y=x^2$) Line (third graph/$y=x^3$) Line (third graph/$y=x^3$)
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')
Warning: Calling legend with multiple outputs will not be supported in a future release.
hIcons =
9x1 graphics array: Text (first graph $y=x$) Text (second graph, $y=x^2$) Text (third graph, $y=x^3$) Line (first graph $y=x$) Line (first graph $y=x$) Line (second graph/$y=x^2$) Line (second graph/$y=x^2$) Line (third graph/$y=x^3$) Line (third graph/$y=x^3$)
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
Stefan
Stefan 2025년 4월 3일
Many thanks! It does the trick.
dpb
dpb 2025년 4월 3일
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... :(

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Graphics Object Properties에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by