轉自:http://blog.sina.com.cn/s/blog_7db803c10102weyk.html
Matlab中legend默認的位置在NorthEast,如圖所示:
%Matlab中legend的位置設置
clc
clear
close all
Npoint = 100;
x = linspace(0,4*pi,Npoint);
y1 = sin(x);
y2 = cos(x);
H = plot(x,y1,x,y2);
legend('sin(x)','cos(x)');
然而,我們卻可以通過Location對legend的位置進行改變,變為North,如圖所示
%Matlab中legend的位置設置
clc
clear
close all
Npoint = 100;
x = linspace(0,4*pi,Npoint);
y1 = sin(x);
y2 = cos(x);
H = plot(x,y1,x,y2);
legend('sin(x)','cos(x)','Location','North');
Matlab位置選擇
設置 | 位置 |
---|---|
'North' | inside plot box near top |
'South' | inside bottom |
'East' | inside right |
'West' | inside left |
'NorthEast' | inside top right (default for 2-D plots) |
'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 (default for 3-D plots) |
'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 |
Matlab中還可以選擇某條曲線legend的指定顯示
%Matlab中legend的選擇
clc
clear
close all
Npoint = 101;
x = linspace(0,10,Npoint);
y1 = besselj(1,x);
y2 = besselj(2,x);
y3 = besselj(3,x);
y4 = besselj(4,x);
y5 = besselj(5,x);
H = plot(x,y1,x,y2,x,y3,x,y4,x,y5);
legend('First','Second','Third','Fourth','Fifth','Location','NorthEastOutside')
如果只想顯示第1、3、5條,也很簡單
%Matlab中legend的選擇
clc
clear
close all
Npoint = 101;
x = linspace(0,10,Npoint);
y1 = besselj(1,x);
y2 = besselj(2,x);
y3 = besselj(3,x);
y4 = besselj(4,x);
y5 = besselj(5,x);
H = plot(x,y1,x,y2,x,y3,x,y4,x,y5);
h1 = legend(H([1 3 5]),'First','Third','Fifthth','Location','NorthEastOutside')
還可以使用Orientation對legend進行橫向排列
%Matlab中legend的橫排,注意,Location位置改變為North
clc
clear
close all
Npoint = 101;
x = linspace(0,10,Npoint);
y1 = besselj(1,x);
y2 = besselj(2,x);
y3 = besselj(3,x);
y4 = besselj(4,x);
y5 = besselj(5,x);
H = plot(x,y1,x,y2,x,y3,x,y4,x,y5);
h1 = legend(H([1 3 5]),'First','Third','Fifthth','Location','North');
set(h1,'Orientation','horizon')
不顯示方框:
clc
clear
close all
Npoint = 101;
x = linspace(0,10,Npoint);
y1 = besselj(1,x);
y2 = besselj(2,x);
y3 = besselj(3,x);
y4 = besselj(4,x);
y5 = besselj(5,x);
H = plot(x,y1,x,y2,x,y3,x,y4,x,y5);
h1 = legend(H([1 3 5]),'First','Third','Fifthth','Location','North');
set(h1,'Orientation','horizon','Box','off')