MATLAB Answers

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

211 views (last 30 days)
Sam Muldoon
Sam Muldoon on 7 Dec 2015
Answered: Arun Krishnadas on 22 Jul 2020
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 Comments
Walter Roberson
Walter Roberson on 7 Dec 2015
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.

Sign in to comment.

Accepted Answer

Jacob D
Jacob D on 14 Nov 2016
Edited: Jacob D on 14 Nov 2016
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;
  2 Comments
Jacob D
Jacob D on 14 Nov 2016
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 =]

Sign in to comment.

More Answers (1)

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

Community Treasure Hunt

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

Start Hunting!