matlab legend 使用
用Matlab畫圖時,有時候需要對各種圖標進行標注,例如,用“+”代表A的運動情況,“*”代表B的
運動情況。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) 來指定圖例標識框的位置
這些是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表示第二個出現的線條,參數字符串確定注釋視窗在圖形中的位置。同時,注釋視窗也可以用鼠標拖動,以便將其放置在一個合適的位置。
【 例 】在同一坐標內,繪出兩條函數曲線並有圖解注釋。
x=0:0.2:12;
plot(x,sin(x),'-',x,1.5*cos(x),':')
legend('First','Second',-1); %強行將注釋視窗放在圖形視窗的外右邊。