How can I modify the length of the lines in a legend?

170 ビュー (過去 30 日間)
MathWorks Support Team
MathWorks Support Team 2010 年 9 月 1 日
編集済み: MathWorks Support Team 2024 年 10 月 16 日
How can I reduce the length of the lines shown in the legend of a MATLAB plot?

採用された回答

MathWorks Support Team
MathWorks Support Team 2024 年 10 月 8 日
編集済み: MathWorks Support Team 2024 年 10 月 16 日
MATLAB R2024b and later:
The following example code illustrates how you can reduce the length of the lines shown in the legend of a plot in MATLAB R2024b and later. Using multiple outputs from the "legend" command is discouraged and will give warnings in MATLAB R2024b. Additionally, you can leverage the legend's "IconColumnWidth" property to adjust the width allocated to the icon of the legend. 
p=plot(1:10); l=legend(p); l.IconColumnWidth = 10;
In cases when all the icons are markers, the value of "IconColumnWidth" will be adjusted to be narrow automatically. In all other cases, it will have its old default value which can still be adjusted. 
scatter(rand(3,10), rand(3,10), 'filled'); l = legend;
MATLAB R2014b to MATLAB R2024a:
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];
 
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 件のコメント
Cris LaPierre
Cris LaPierre 2021 年 1 月 28 日
Moving comment from Deepak Sapkota accidentally added as a flag.
Thank you Quan. Your code worked for me to change the legend length.
Rik
Rik 2021 年 2 月 4 日
@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];

サインインしてコメントする。

その他の回答 (2 件)

Brandon Ballard
Brandon Ballard 2021 年 7 月 17 日
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):
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.
quan zhang on 17 Sep 2019
I can only modulate the length, not the width by using" leg = legend('Plot1','Plot2',...);
leg.ItemTokenSize = [x1,x2];"

Afiq Azaibi
Afiq Azaibi 2024 年 10 月 7 日
Using multiple outputs from the legend command is discouraged and will start warning in R2024b. Also starting in R2024b, you can leverage the legend's IconColumnWidth property to adjust the width allocated to the icon of the legend.
p=plot(1:10);
l=legend(p);
l.IconColumnWidth = 10;
In cases when all the icons are markers, the value of IconColumnWidth will be adjusted to be narrow automatically. In all other cases, it will have its old default value which can still be adjusted.
scatter(rand(3,10), rand(3,10), 'filled');
l = legend;

カテゴリ

Help Center および File ExchangeCreating, Deleting, and Querying Graphics Objects についてさらに検索

製品


Translated by