Matlab中legend函数详解及legend边框去掉的方法

做一个柔情的程序猿

共 6767字,需浏览 14分钟

 · 2022-06-07

🚩legend函数的基本用法

⭐️用法一

LEGEND(string1,string2,string3, ...)

分别将字符串1、字符串2、字符串3……标注到图中,每个字符串对应的图标为画图时的图标

例如:

plot(x,sin(x),'.b',x,cos(x),'+r'

legend('sin','cos')

作用是可以把"."标识为'sin',把"+"标识为"cos" 。

⭐️用法二

LEGEND(...,'Location',LOC) %指定图例标识框的位置

具体LOC可以通过 Matlab help 进行查看。

后面一段是对应的翻译和说明

'North' inside plot box near top

'South' inside bottom

'East' inside right

'West' inside left

'NorthEast' inside top right (default)

'NorthWest inside top left

'SouthEast' inside bottom right

'SouthWest' inside bottom left

'NorthOutside' outside plot box near top

'SouthOutside' outside bottom

'EastOutside' outside right

'WestOutside' outside left

'NorthEastOutside' outside top right

'NorthWestOutside' outside top left

'SouthEastOutside' outside bottom right

'SouthWestOutside' outside bottom left

'Best' least conflict with data in plot

'BestOutside' least unused space outside plot

'North' 图例标识放在图顶端

'South' 图例标识放在图底端

'East' 图例标识放在图右方

'West' 图例标识放在图左方

'NorthEast' 图例标识放在图右上方(默认)

'NorthWest 图例标识放在图左上方

'SouthEast' 图例标识放在图右下角

'SouthWest' 图例标识放在图左下角

(以上几个都是将图例标识放在框图内)


'NorthOutside' 图例标识放在图框外侧上方

'SouthOutside' 图例标识放在图框外侧下方

'EastOutside' 图例标识放在图框外侧右方

'WestOutside' 图例标识放在图框外侧左方

'NorthEastOutside' 图例标识放在图框外侧右上方

'NorthWestOutside' 图例标识放在图框外侧左上方

'SouthEastOutside' 图例标识放在图框外侧右下方

'SouthWestOutside' 图例标识放在图框外侧左下方

(以上几个将图例标识放在框图外)


'Best' 图标标识放在图框内不与图冲突的最佳位置

'BestOutside' 图标标识放在图框外使用最小空间的最佳位置

例如:

legend('sin','cos','location','northwest')

作用是可以将标识框放置在图的左上角。

⭐️用法三

当在一个坐标系上画多幅图形时,为区分各个图形,Matlab提供了图例的注释说明函数。其格式如下:

legend(字符串1,字符串2,字符串3,...,参数)

参数字符串的含义如下表所示:

参数字符串                                  含义
0                      尽量不与数据冲突,自动放置在最佳位置
1                                      放置在图形的右上角
2                                      放置在图形的左上角
3                                      放置在图形的左下角
4                                      放置在图形的右下角
-1                                  放置在图形视窗的外右边

此函数在图中开启了一个注释视窗,依据绘图的先后顺序,依据输出字符串对各个图形进行注释说明。

如字符串1表示第一个出现的线条,字符串2表示第二个出现的线条,参数字符串确定注释视窗在图形中的位置。同时,注释视窗也可以用鼠标拖动,以便将其放置在一个合适的位置。


🚩示例

🍊在当前坐标区上添加图例

绘制两个线条并在当前坐标区上添加一个图例。将图例标签指定为 legend 函数的输入参数。

x = linspace(0,pi);

y1 = cos(x);

plot(x,y1)

hold on

y2 = cos(2*x);

plot(x,y2)

legend('cos(x)','cos(2x)')

 

如果您在坐标区添加或删除数据序列,图例会相应地更新。创建数据序列时,可通过将 DisplayName 属性设置为名称-值对组来控制新数据序列的标签。如果您不指定标签,则图例使用 'dataN' 形式的标签。

注意:如果您不希望在坐标区中添加或删除数据序列时自动更新图例,可将图例的 AutoUpdate 属性设置为 'off'

y3 = cos(3*x);

plot(x,y3,'DisplayName','cos(3x)')

hold off

删除图例。

legend('off')

🍊在特定坐标区上添加图例 

从 R2019b 开始,您可以使用 tiledlayout 和 nexttile 函数显示分块图。调用 tiledlayout 函数以创建一个 2×1 分块图布局。调用 nexttile 函数以创建坐标区对象 ax1 和 ax2。在每个坐标区中对随机数据绘图。通过指定 ax1 作为 legend 的第一个输入参数,在上部绘图中添加一个图例。

tiledlayout(2,1)

y1 = rand(3);

ax1 = nexttile;

plot(y1)

y2 = rand(5);

ax2 = nexttile;

plot(y2)

legend(ax1,{'Line 1','Line 2','Line 3'})

🍊在执行绘图命令的过程中指定图例标签

绘制两个线条。通过将 DisplayName 属性设置为所需的文本,在执行绘图命令的过程中指定图例标签。然后,添加一个图例。

x = linspace(0,pi);

y1 = cos(x);

plot(x,y1,'DisplayName','cos(x)')

hold on

y2 = cos(2*x);

plot(x,y2,'DisplayName','cos(2x)')

hold off

legend

🍊从图例中排除线条 

要从图例中排除一个线条,请将其标签指定为空字符向量或字符串。例如,绘制两个正弦波,并通过调用 yline 函数在零位置添加一条水平虚线。然后创建一个图例,并通过将其标签指定为 '' 来排除该零位置的虚线。

x = 0:0.2:10;

plot(x,sin(x),x,sin(x+1));

hold on

yline(0,'--')

legend('sin(x)','sin(x+1)','')

🍊图例位置和列数

绘制四个线条。在坐标区的西北角创建一个图例。使用 NumColumns 属性指定图例的列数。

x = linspace(0,pi);

y1 = cos(x);

plot(x,y1)

hold on

y2 = cos(2*x);

plot(x,y2)

y3 = cos(3*x);

plot(x,y3)

y4 = cos(4*x);

plot(x,y4)

hold off

legend({'cos(x)','cos(2x)','cos(3x)','cos(4x)'},...

'Location','northwest','NumColumns',2)

默认情况下,图例项逐列从上到下排列。要逐行从左到右排列图例项,请将 Orientation 属性设置为 'horizontal'

🍊在分块图布局中显示共享图例

当您要在两个或多个图之间共享一个图例时,您可以在布局的一个单独图块中显示该图例。您可以将图例放置在图块网格中,或放置在外侧图块中。

在一个分块图布局中创建三个绘图。

t = tiledlayout('flow','TileSpacing','compact');

nexttile

plot(rand(5))

nexttile

plot(rand(5))

nexttile

plot(rand(5))

添加一个共享图例,并将其移至第四个图块。 

lgd = legend;

lgd.Layout.Tile = 4;

接下来,添加第四个绘图,并将图例移到 east 图块。 

nexttile

plot(rand(5))

lgd.Layout.Tile = 'east';

🍊在图例中包含部分图形对象

如果您不想将绘制的所有图形对象都包含在图例中,可以指定要包含的图形对象。

绘制三个线条并返回创建的 Line 对象。创建只包含其中两条线的图例。将第一个输入参数指定为要包含的 Line 对象的向量。

x = linspace(0,pi);

y1 = cos(x);

p1 = plot(x,y1);

hold on

y2 = cos(2*x);

p2 = plot(x,y2);

y3 = cos(3*x);

p3 = plot(x,y3);

hold off

legend([p1 p3],{'First','Third'})

🍊创建包含 LaTeX 标记的图例 

创建一个图,通过调用 legend 函数并将 Interpreter 属性设置为 'latex' 来添加包含 LaTeX 标记的图例。用美元符号 ($) 将标记括起来。

x = 0:0.1:10;

y = sin(x);

dy = cos(x);

plot(x,y,x,dy);

legend('$sin(x)$','$\frac{d}{dx}sin(x)$','Interpreter','latex');

🍊为图例添加标题 

绘制两个线条并创建一个图例。然后为图例添加标题。

x = linspace(0,pi);

y1 = cos(x);

plot(x,y1)


hold on

y2 = cos(2*x);

plot(x,y2)

hold off


lgd = legend('cos(x)','cos(2x)');

title(lgd,'My Legend Title')

🍊删除图例背景

绘制两个线条并在坐标区左下角创建一个图例。然后,删除图例的背景和轮廓。

x = linspace(0,pi);

y1 = cos(x);

plot(x,y1)


hold on

y2 = cos(2*x);

plot(x,y2)

hold off


legend({'cos(x)','cos(2x)'},'Location','southwest')

legend('boxoff')

🍊修改图例外观 

通过设置 Legend 属性来修改图例外观。创建图例时,可以在 legend 命令中使用名称-值对组来设置属性。还可以在创建图例后使用 Legend 对象来设置属性。

绘制四行随机数据。创建图例并将 Legend 对象赋给变量 lgd。使用名称-值对组设置 FontSize 和 TextColor 属性。

rdm = rand(4);

plot(rdm)

lgd = legend({'Line 1','Line 2','Line 3','Line 4'},...

'FontSize',12,'TextColor','blue');

创建图例后,通过引用 lgd 来修改图例。使用对象圆点属性名称表示法设置 NumColumns 属性。 

lgd.NumColumns = 2;

 🍊其他

🎉在同一坐标内,绘出两条函数曲线并有图解注释。

x=0:0.2:12;

plot(x,sin(x),'-',x,1.5*cos(x),':')

legend('First','Second',-1); %强行将注释视窗放在图形视窗的外右边。

 

🎉

x = 0:.2:12; plot(x,bessel(1,x),x,bessel(2,x),x,bessel(3,x)); legend('First','Second','Third'); legend('First','Second','Third','Location','NorthEastOutside')  b = bar(rand(10,5),'stacked');colormap(summer); hold on x = plot(1:10,5*rand(10,1),'marker','square','markersize',12,...  'markeredgecolor','y','markerfacecol or',[.6 0 .6],... 'linestyle','-','color','r','linewidth',2);  hold off legend([b,x],'Carrots','Peas','Peppers','Green Beans',... 'Cucumbers','Eggplant')

 🎉

m=400; k=30000; phaser=0.1; A=20; Cratio=[0.1,0.3,0.5,0.8,1]n=length(cratio); t=0:0.01:8; cols=['r','m','g','c','b','y','k']; for nn=1:n     aratio=-cratio(nn)*sqrt(k/m);     wd=sqrt(k/m)*sqrt(1-cratio(nn)^2);     z=A*exp(aratio*t).*sin(wd*t+phaser);     plot(t,z,cols(nn));     jfd(nn)={'阻尼比='};     dhd(nn)={num2str(cratio(nn))};     hold onendxlabel('时间'); ylabel('幅值') jus=strcat(jfd,dhd); legend(jus(1:n)')   grid on; hold off;

 


🚩去除legend边框

⭐️问题描述

MATLAB绘图中,默认的legend设置,总会显⽰边框,影响图像显⽰,如何去除呢?

 

⭐️解决方案 

方法一:

h = legend('x')

set(h,'box','off')

方法二:

legend boxoff

例如:

t = 0:0.01:2*pi;

figure

plot(t,sin(t))

h = legend('x');

set(h,'box','off')


推荐阅读

(点击标题可跳转阅读)

python程序设计思想

【初学不要怕】教你全方位理解python函数及其使用(包括lambda函数和递归函数详解系列)

【加解密算法实现】全面剖析RSA加解密算法(附完整C/Python源码)

详解python的运行方式

强推10款Python常用的开发工具

【专家推荐】保姆级开源工具推荐,一用一个爽,非常劲爆(收藏系列)

【恭喜考研拟录取】极力推荐科研必备软件,让你科研生涯事半功倍

带你全面剖析python自然语言处理(NLP系列一)

带你全面剖析python自然语言处理(NLP技术要点)

带你全面剖析python自然语言处理(TF-IDF和TextRank)

老铁,三连支持一下,好吗?↓↓↓


点分享

点点赞

点在看

浏览 150
点赞
评论
收藏
分享

手机扫一扫分享

举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

举报