MATLAB: How to modify the lengh of the lines in a LEGEND

changedwhhgwaitMATLABshrinkspecify

I would like to reduce the length of the lines shown in the legend of a plot in MATLAB.

Best Answer

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)])