How to increase the length of the legend line?
Show older comments
Hello, I would like to increase the length (not linewidth) of the line inside the legend. Does anyone has an idea? Thanks you.
t = [0:0.1:2*pi]
a = sin(t);
b = cos(t);
plot(t,a,t,b,'linewidth',3)
l = legend({'Sinus','Cosinus'},...
'location','southwest','FontName','TimesNewRoman','FontSize',20);

1 Comment
KALYAN ACHARJYA
on 21 Apr 2018

Answers (3)
KALYAN ACHARJYA
on 21 Apr 2018
Edited: Walter Roberson
on 8 Sep 2020
2 votes
I don't think it is customizable directly, go to the following links to user-defined function-
4 Comments
Arpan Sircar
on 29 Mar 2019
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.
Minh Chien Trinh
on 24 Jul 2019
Thank you Arpan Sircar.
It works well.
Luca Menegozzo
on 28 Nov 2020
Edited: Luca Menegozzo
on 28 Nov 2020
The best solution to the problem without extra functions. Thank you!
Tin Truong Chanh
on 13 Jan 2021
thank you Arpan Sircar
Starting in R2024b, you can leverage the IconColumnWidth property on legend to increase the length of the line:
t = [0:0.1:2*pi]
a = sin(t);
b = cos(t);
plot(t,a,t,b,'linewidth',3)
l = legend({'Sinus','Cosinus'},...
'location','southwest','FontName','TimesNewRoman','FontSize',20);
l.IconColumnWidth = 60;
Categories
Find more on Legend in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!