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

157 次查看(过去 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 中查找有关 Graphics Object Properties 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by