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
水平排列
h1 = legend(H([1 3 5]),'First','Third','Fifthth','Location','North');
set(h1,'Orientation','horizon')
不要方框
PS:不要方框,此时图例为透明的颜色,不会遮挡住数据线条
h1 = legend(H([1 3 5]),'First','Third','Fifthth','Location','North');
set(h1,'Orientation','horizon','Box','off')
参考连接
http://blog.sina.com.cn/s/blog_7db803c10102weyk.html
legend详细介绍
PS:legend(vsb1)也可设置图例的:隐藏、表现、介于两者之间,没试出其效果
如果想要在一张图上显示2个图例,需要加上下面这句话
ah=axes('position',get(gca,'position'),'visible','off');
添加颜色、大小等
% 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');
参考链接:https://ww2.mathworks.cn/help/matlab/ref/legend.html