1. 程式人生 > >MATLAB作圖的圖例控制

MATLAB作圖的圖例控制

matlab繪圖中legend的終極用法,去掉legend的邊框 (2013-06-17 16:15:40)轉載▼
標籤: 教育 分類: Matlab學習
matlab繪圖中legend的終極用法
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%持續更新。 當前: 20100108 %
%僅作筆記。 作者: keyflying %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
legend有時候挺煩人的,儘管大多時候挺好用。
基本資料:
data = rand(25)+repmat(1:25,25,1);
H = plot(data);
基本用法:
legend({‘str1’,’str2’,’strn’});
高階用法1:指定legend顯示的位置:
legend({‘str1’,’str2’,’strn’},1);
legend({‘str1’,’str2’,’strn’},2);
legend({‘str1’,’str2’,’strn’},’Location’,’SouthEast’);
可選的位置很多:
North:Inside plot box near top
South:Inside bottom
EastI:nside 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:Outsideright
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
通常,用’Best‘比較不錯
高階用法2:指定顯示某幾條曲線的legend:
方法1:複雜到吐血
例如你有25條曲線,想顯示其中1,6,11,16,21的legend,則
for i = [2:5 7:10 12:15 17:20 22:25]
set(get(get(H(i),’Annotation’),’LegendInformation’),’IconDisplayStyle’,’off’);
end
legend(‘1’,’6’,’11’,’16’,’21’);
方法2:簡單到鬱悶
H = plot(data);
legend(H([1 6 11 16 21],’1,’6’,’11’,’16’,’21’);
高階用法3:legend橫排
hl = legend(H([1 6 11 16 21],’1,’6’,’11’,’16’,’21’);
set(hl,’Orientation’,’horizon’)
高階用法4:不顯示方框:
hl = legend(H([1 6 11 16 21],’1,’6’,’11’,’16’,’21’);
set(hl,’Box’,’off’);
參考文獻

http://www.mathworks.cn/access/helpdesk/help/techdoc/creating_plots/braliom.html
doc legend
Site:http://hi.baidu.com/keyflying/item/7b39e9bbdcaceba5ebba936f
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
另外,去掉legend邊框,可以參考:http://www.newsmth.net/nForum/#!article/Paper/62848
legend(‘x’,’y’)
legend(‘boxoff’)

寫兩次就好了。我最初嘗試著寫成legend(‘x’,’y’,’boxoff’)是不行的。
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
修改legend的字型:
legend1=legend(‘x’,’y’);
set(legend1,’FontName’,’Times New Roman’,’FontSize’,12,’FontWeight’,’normal’);
修改legend位置:
i=0;%i=0,自動調整最佳位置;i=1,右上;i=2,左上;i=3,左下;i=4,右下;
legend1=legend(‘x’,’y’,i);
20160518
~

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
最好的說明文件,當然是幫助文件:
matlab:
doc legend

legend(‘hide’), legend(axes_handle,’hide’) makes the legend in the current axes or the axes specified by axes_handle invisible.
legend(‘show’), legend(axes_handle,’show’) makes the legend in the current axes or the axes specified by axes_handle visible. A legend is created if one did not exist previously. Legends created automatically are limited to depict only the first 20 lines in the plot; if you need more legend entries, you can manually create a legend for them all with legend(‘string1’,’string2’,…) syntax.
legend(‘boxoff’), legend(axes_handle,’boxoff’) removes the box from the legend in the current axes or the axes specified by axes_handle, and makes its background transparent.
legend(‘boxon’), legend(axes_handle,’boxon’) adds a box with an opaque background to the legend in the current axes or the axes specified by axes_handle.