How does one Increase spacing between different entries in a plot legend?

조회 수: 80 (최근 30일)
If you look at the legend on the left it's hard to tell when one legend entry ends and the next begins. The spacing on the right is a bit too extreme, but hopefully you get the idea. I want to learn how to insert a line break between different legend entries.
There's already a posting with a title similair to this (click here). However, The person was trying to embed their plot in a Latex document, and I'm not doing anything like that. Also their code contains a lot of dollar signs and things I don't understand. Here's my code:
line_handles = [h1, h2, h3, h3];
t1 = 'Previously localized sensors';
t2 = ['Previously localized sensors', sprintf('\n'),...
'being used to locate the next sensor'];
t3 = ['Sensors of unknown location'];
t4 = ['Sensor whose location we' sprintf('\n') 'are presently determining'];
legend_texts = {t1, t2, t3, t4};
h_leg = legend(line_handles, legend_texts);
legend(h_axes,'boxon');
legend(h_axes,'show');
h_leg.FontSize = 22.5;
h_leg.Location = 'bestoutside';
The reason I passed handles of chart line objects to the legend function is that I don't want a legend entry for every chart line object which is being plotted. I only want entries in the legend for four out of many data series which are in this plot.
The following change doesn't affect the spacing at all:
t1 = ['text' sprintf('\n') ' ' sprintf('\n')...
' ' sprintf('\n') ' ' sprintf('\n')...
' ' sprintf('\n') ' '];
  댓글 수: 2
Sam Muldoon
Sam Muldoon 2015년 12월 7일
편집: Sam Muldoon 2015년 12월 7일
Okay, it looks like trailing white space chars are thrown away, but prefixed white space chars are not. So,
t2 = [' ' sprintf('\n') 'text'];
has an affect, whereas...
t2 = ['text' sprintf('\n') ' '];
... produces the same result as:
t2 = 'text';
However, the results of stuff like
t2 = [' ' sprintf('\n') 'text'];
look strange...
Walter Roberson
Walter Roberson 2015년 12월 7일
Testing in R2014a (before the change to the graphics system) it appears that if you use a trailing non-printable character like \h then spacing is introduced, but the sample icon will be centered on all of the lines considered to be present.
I do not know the capabilities with legend from R2014b onward.

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

채택된 답변

Jacob D
Jacob D 2016년 11월 14일
편집: Jacob D 2016년 11월 14일
I know this was quite some time ago now but you may try the following code to increase the total height of the legend, which will, conveniently, increase the spacing between entries too.
HeightScaleFactor = 1.5;
NewHeight = h_leg.Position(4) * HeightScaleFactor;
h_leg.Position(2) = h_leg.Position(2) - (NewHeight - h_leg.Position(4));
h_leg.Position(4) = NewHeight;
So for completeness, with your example:
f1=figure;h_axes=gca;
h1=plot(0,0,'ok'); hold on;
h2=plot(0,0,'ob');
h3=plot(0,0,'ok','MarkerFaceColor','k');
h4=plot(0,0,'or','MarkerFaceColor','r'); hold off;xlim(1:2);
line_handles = [h1, h2, h3, h4];
t1 = 'Previously localized sensors';
t2 = ['Previously localized sensors', sprintf('\n'),...
'being used to locate the next sensor'];
t3 = ['Sensors of unknown location'];
t4 = ['Sensor whose location we' sprintf('\n') 'are presently determining'];
legend_texts = {t1, t2, t3, t4};
h_leg = legend(line_handles, legend_texts);
h_axes.Visible='off';
legend(h_axes,'boxon');
legend(h_axes,'show');
h_leg.FontSize = 22.5;
h_leg.Location = 'bestoutside';
f2=figure;objects=allchild(f1);copyobj(get(f1,'children'),f2);
HeightScaleFactor = 1.5;
NewHeight = h_leg.Position(4) * HeightScaleFactor;
h_leg.Position(2) = h_leg.Position(2) - (NewHeight - h_leg.Position(4));
h_leg.Position(4) = NewHeight;
  댓글 수: 3
Jacob D
Jacob D 2016년 11월 14일
Not sure when you checked the answer but I have edited since and I think it is now doing what you suggest. I only just saw your comment now =]
Adam Watts
Adam Watts 2024년 5월 23일
When the parent axis is a UIAxes object within a UI GridLayout, one cannot adjust the position. You get the following Warning:
Unrecognized method, property, or field 'Position_I' for class 'matlab.ui.container.GridLayout'.

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

추가 답변 (1개)

Arun Krishnadas
Arun Krishnadas 2020년 7월 22일
A simple method would be to add \newline in a new line on each legend entry, which worked in one case for me.

카테고리

Help CenterFile Exchange에서 Legend에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by