matlab繪圖中legend的終極用法

matlab繪圖中legend的終極用法

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%ide

%持續更新。 當前: 20100108 %spa

%僅做筆記。 做者: keyflying %orm

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%get

legend有時候挺煩人的,儘管大多時候挺好用。it

基本數據:io

data = rand(25)+repmat(1:25,25,1);
H = plot(data);ast

基本用法:form

legend({'str1','str2','strn'});class

高級用法1:指定legend顯示的位置:sed

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

相關文章
相關標籤/搜索