【 MATLAB 】legend 的使用简析

您所在的位置:网站首页 legend什么意思怎么读 【 MATLAB 】legend 的使用简析

【 MATLAB 】legend 的使用简析

2024-07-11 12:00| 来源: 网络整理| 查看: 265

目录

legend

在作图命令中(plot)给出图例标签;

legend(label1,...,labelN)

给当前轴添加图例;

legend(target,___)

给特定轴添加图例;

legend(subset,___)

给部分函数添加图例;

legend(___,'Location',lcn)

指定图例的位置(方向)以及显示的列数;

lgd = legend(___)

给图例添加标题;

legend('boxoff')

去除图例边框;

Modify Legend Appearance

修改图例的外观;

MATLAB 中 legend 可以翻译为图例,下面逐一用例子来说明 legend 的用法,并辅以必要的说明。

我只挑选几种使用的来说明。

legend 在作图命令中(plot)给出图例标签;

Plot two lines. Specify the legend labels during the plotting commands by setting the DisplayName property to the desired text. Then, add a legend.

画两条线。 通过将DisplayName属性设置为所需文本,在绘图命令期间指定图例标签。 然后,添加一个图例(legend)。

% Plot two lines. % Specify the legend labels during the plotting commands by setting the DisplayName property to the desired text. % Then, add a legend. clear clc close all 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

对代码做如下改动,将两幅图分开画,然后添加图例的方法:

% Plot two lines. % Specify the legend labels during the plotting commands by setting the DisplayName property to the desired text. % Then, add a legend. clear clc close all x = linspace(0,pi); y1 = cos(x); subplot(2,1,1); plot(x,y1,'DisplayName','cos(x)') legend %hold on y2 = cos(2*x); subplot(2,1,2); plot(x,y2,'DisplayName','cos(2x)') % hold off legend

legend(label1,...,labelN) 给当前轴添加图例;

legend(label1,...,labelN) sets the legend labels. Specify the labels as a list of character vectors or strings, such as legend('Jan','Feb','Mar').

legend(label1,...,labelN)设置图例标签。 将标签指定为字符向量或字符串列表,例如legend('Jan','Feb','Mar')。

绘制两条线并在当前轴上添加图例。 将图例标签指定为图例功能的输入参数。

% Plot two lines and add a legend to the current axes. % Specify the legend labels as input arguments to the legend function. clear clc close all x = linspace(0,pi); y1 = cos(x); plot(x,y1) hold on y2 = cos(2*x); plot(x,y2) legend('cos(x)','cos(2x)')

将程序稍作改动,将这两个正弦函数的图像画在不同的子图中,并添加标签:

% Plot two lines and add a legend to the current axes. % Specify the legend labels as input arguments to the legend function. clear clc close all x = linspace(0,pi); y1 = cos(x); subplot(2,1,1); plot(x,y1) legend('cos(x)') % hold on y2 = cos(2*x); subplot(2,1,2); plot(x,y2) legend('cos(2x)')

legend(target,___) 给特定轴添加图例; % Create a figure with two subplots and return the two Axes objects, ax1 and ax2. % Plot random data in each subplot. Add a legend to the upper subplot by specifying ax1 as the first input argument to legend. clear clc close all x = linspace(0,pi); y1 = rand(3); ax1 = subplot(2,1,1); plot(y1) y2 = rand(5); ax2 = subplot(2,1,2); plot(y2) legend(ax1,{'Line 1','Line 2','Line 3'})

legend(subset,___) 给部分函数添加图例;

subset 是子集的意思,它表示只给这个子集中的函数添加图例,后面的——表示,这些图例的标签,即它们的名字是什么?

下面给出一个实例,参考起来很容易理解:

% If you do not want to include all of the plotted graphics objects in the legend, % then you can specify the graphics objects that you want to include. % Plot three lines and return the Line objects created. % Create a legend that includes only two of the lines. % Specify the first input argument as a vector of the Line objects to include. clear clc close all 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],{'cos(x)','cos(3x)'})

legend(___,'Location',lcn) 指定图例的位置(方向)以及显示的列数; % Plot four lines. Create a legend in the northwest area of the axes. % Specify the number of legend columns using the NumColumns property. clear clc close all 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)

lgd = legend(___) 给图例添加标题; % Plot two lines and create a legend. Then, add a title to the legend. clear clc close all 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')

legend('boxoff') 去除图例边框; % Plot two lines and create a legend in the lower left corner of the axes. % Then, remove the legend background and outline. clear clc close all 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')

Modify Legend Appearance 修改图例的外观; % Modify the legend appearance by setting Legend properties. clear clc close all rdm = rand(4); plot(rdm) lgd = legend({'Line 1','Line 2','Line 3','Line 4'},'FontSize',12,'TextColor','blue')

当然也可以通过下面的方式来修改:

% Modify the legend appearance by setting Legend properties. clear clc close all rdm = rand(4); plot(rdm) lgd = legend('Line 1','Line 2','Line 3','Line 4'); lgd.FontSize = 12; lgd.TextColor = 'blue';

效果是一样的。

下面多改几下:

% Modify the legend appearance by setting Legend properties. clear clc close all rdm = rand(4); plot(rdm) lgd = legend('Line 1','Line 2','Line 3','Line 4'); lgd.FontSize = 12; lgd.TextColor = 'blue'; lgd.NumColumns = 2; lgd.Location = 'southwest'; leg.Orientation = 'vertical'; title(lgd,'My Legend Title');

 

 

 



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3