1. 程式人生 > >【 MATLAB 】legend 的使用簡析

【 MATLAB 】legend 的使用簡析

目錄

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

給當前軸新增圖例;

 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');