Borrar filtros
Borrar filtros

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

188 visualizaciones (últimos 30 días)
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 comentarios
Sam Muldoon
Sam Muldoon el 7 de Dic. de 2015
Editada: Sam Muldoon el 7 de Dic. de 2015
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 el 7 de Dic. de 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.

Iniciar sesión para comentar.

Respuesta aceptada

Jacob D
Jacob D el 14 de Nov. de 2016
Editada: Jacob D el 14 de Nov. de 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;
  3 comentarios
Jacob D
Jacob D el 14 de Nov. de 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 =]
Adam Watts
Adam Watts el 23 de Mayo de 2024
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'.

Iniciar sesión para comentar.

Más respuestas (1)

Arun Krishnadas
Arun Krishnadas el 22 de Jul. de 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!

Translated by