How can I modify the lengh of the lines in a LEGEND?
344 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
MathWorks Support Team
el 1 de Sept. de 2010
Respondida: Brandon Ballard
el 17 de Jul. de 2021
I would like to reduce the length of the lines shown in the legend of a plot in MATLAB.
Respuesta aceptada
MathWorks Support Team
el 17 de Mayo de 2018
Editada: MathWorks Support Team
el 17 de Mayo de 2018
The following example code illustrates how you can reduce the length of the lines shown in the legend of a plot:
In MATLAB R2014b and later
h1 = plot(1:10);
[hh,icons,plots,txt] = legend({'Line 1'});
p1 = icons(1).Position;
icons(1).Position = [0.3 p1(2) 0];
icons(2).XData = [0.05 0.2];
In MATLAB R2014a and before
plot(rand(2))
lh = legend('Line 1','Line 2');
ch = get(lh,'children');
xd = get(ch(2),'XData');
set(ch(2),'XData',[xd(1)+0.2 xd(2)])
3 comentarios
Cris LaPierre
el 28 de En. de 2021
Thank you Quan. Your code worked for me to change the legend length.
Rik
el 4 de Feb. de 2021
@Gergely Szabó did you use the code for R2014a and before? As you can see below, the example runs as expected on R2020b.
h1 = plot(1:10);
[hh,icons,plots,txt] = legend({'Line 1'});
h2 = plot(1:10);
[hh,icons,plots,txt] = legend({'Line 1'});
p2 = icons(1).Position;
icons(1).Position = [0.3 p2(2) 0];
icons(2).XData = [0.05 0.2];
Más respuestas (1)
Brandon Ballard
el 17 de Jul. de 2021
Just to make it easier for people to find I have copied the approach listed in the comments which works for versions of Matlab R2018b onwards (I am currently using Matlab 2021a):
The simplest way to do it is to use: -
leg = legend('Plot1','Plot2',...);
leg.ItemTokenSize = [x1,x2];
By default x1=30 and x2=18 so put larger or smaller numbers as x1,x2 to increase or decrease the legend line size.
I can only modulate the length, not the width by using" leg = legend('Plot1','Plot2',...);
leg.ItemTokenSize = [x1,x2];"
0 comentarios
Ver también
Categorías
Más información sobre Legend en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!