Matlab中l(wèi)egend函數(shù)詳解及l(fā)egend邊框去掉的方法
??legend函數(shù)的基本用法
??用法一
LEGEND(string1,string2,string3, ...)plot(x,sin(x),'.b',x,cos(x),'+r')?
legend('sin','cos')
??用法二
LEGEND(...,'Location',LOC) %指定圖例標(biāo)識(shí)框的位置'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' 圖例標(biāo)識(shí)放在圖頂端
'South' 圖例標(biāo)識(shí)放在圖底端
'East' 圖例標(biāo)識(shí)放在圖右方
'West' 圖例標(biāo)識(shí)放在圖左方
'NorthEast' 圖例標(biāo)識(shí)放在圖右上方(默認(rèn))
'NorthWest 圖例標(biāo)識(shí)放在圖左上方
'SouthEast' 圖例標(biāo)識(shí)放在圖右下角
'SouthWest' 圖例標(biāo)識(shí)放在圖左下角
(以上幾個(gè)都是將圖例標(biāo)識(shí)放在框圖內(nèi))
'NorthOutside' 圖例標(biāo)識(shí)放在圖框外側(cè)上方
'SouthOutside' 圖例標(biāo)識(shí)放在圖框外側(cè)下方
'EastOutside' 圖例標(biāo)識(shí)放在圖框外側(cè)右方
'WestOutside' 圖例標(biāo)識(shí)放在圖框外側(cè)左方
'NorthEastOutside' 圖例標(biāo)識(shí)放在圖框外側(cè)右上方
'NorthWestOutside' 圖例標(biāo)識(shí)放在圖框外側(cè)左上方
'SouthEastOutside' 圖例標(biāo)識(shí)放在圖框外側(cè)右下方
'SouthWestOutside' 圖例標(biāo)識(shí)放在圖框外側(cè)左下方
(以上幾個(gè)將圖例標(biāo)識(shí)放在框圖外)
'Best' 圖標(biāo)標(biāo)識(shí)放在圖框內(nèi)不與圖沖突的最佳位置
'BestOutside' 圖標(biāo)標(biāo)識(shí)放在圖框外使用最小空間的最佳位置
legend('sin','cos','location','northwest')??用法三
legend(字符串1,字符串2,字符串3,...,參數(shù))??示例
??在當(dāng)前坐標(biāo)區(qū)上添加圖例
x = linspace(0,pi);
y1 = cos(x);
plot(x,y1)
hold on
y2 = cos(2*x);
plot(x,y2)
legend('cos(x)','cos(2x)')
y3 = cos(3*x);
plot(x,y3,'DisplayName','cos(3x)')
hold off
legend('off')??在特定坐標(biāo)區(qū)上添加圖例?
tiledlayout(2,1)
y1 = rand(3);
ax1 = nexttile;
plot(y1)
y2 = rand(5);
ax2 = nexttile;
plot(y2)
legend(ax1,{'Line 1','Line 2','Line 3'})
??在執(zhí)行繪圖命令的過程中指定圖例標(biāo)簽
x = linspace(0,pi);
y1 = cos(x);
plot(x,y1,'DisplayName','cos(x)')
hold on
y2 = cos(2*x);
plot(x,y2,'DisplayName','cos(2x)')
hold off
legend
??從圖例中排除線條?
x = 0:0.2:10;
plot(x,sin(x),x,sin(x+1));
hold on
yline(0,'--')
legend('sin(x)','sin(x+1)','')
??圖例位置和列數(shù)
x = linspace(0,pi);
y1 = cos(x);
plot(x,y1)
hold on
y2 = cos(2*x);
plot(x,y2)
y3 = cos(3*x);
plot(x,y3)
y4 = cos(4*x);
plot(x,y4)
hold off
legend({'cos(x)','cos(2x)','cos(3x)','cos(4x)'},...
'Location','northwest','NumColumns',2)
??在分塊圖布局中顯示共享圖例
t = tiledlayout('flow','TileSpacing','compact');
nexttile
plot(rand(5))
nexttile
plot(rand(5))
nexttile
plot(rand(5))
lgd = legend;
lgd.Layout.Tile = 4;
nexttile
plot(rand(5))
lgd.Layout.Tile = 'east';
??在圖例中包含部分圖形對(duì)象
x = linspace(0,pi);
y1 = cos(x);
p1 = plot(x,y1);
hold on
y2 = cos(2*x);
p2 = plot(x,y2);
y3 = cos(3*x);
p3 = plot(x,y3);
hold off
legend([p1 p3],{'First','Third'})
??創(chuàng)建包含 LaTeX 標(biāo)記的圖例?
x = 0:0.1:10;
y = sin(x);
dy = cos(x);
plot(x,y,x,dy);
legend('$sin(x)$','$\fracgo7utgvlrp{dx}sin(x)$','Interpreter','latex');
??為圖例添加標(biāo)題?
x = linspace(0,pi);
y1 = cos(x);
plot(x,y1)
hold on
y2 = cos(2*x);
plot(x,y2)
hold off
lgd = legend('cos(x)','cos(2x)');
title(lgd,'My Legend Title')
??刪除圖例背景
x = linspace(0,pi);
y1 = cos(x);
plot(x,y1)
hold on
y2 = cos(2*x);
plot(x,y2)
hold off
legend({'cos(x)','cos(2x)'},'Location','southwest')
legend('boxoff')
??修改圖例外觀?
rdm = rand(4);
plot(rdm)
lgd = legend({'Line 1','Line 2','Line 3','Line 4'},...
'FontSize',12,'TextColor','blue');
lgd.NumColumns = 2;???其他
x=0:0.2:12;
plot(x,sin(x),'-',x,1.5*cos(x),':')
legend('First','Second',-1); %強(qiáng)行將注釋視窗放在圖形視窗的外右邊。
x = 0:.2:12;plot(x,bessel(1,x),x,bessel(2,x),x,bessel(3,x));legend('First','Second','Third');legend('First','Second','Third','Location','NorthEastOutside')b = bar(rand(10,5),'stacked');colormap(summer);hold onx = plot(1:10,5*rand(10,1),'marker','square','markersize',12,...'markeredgecolor','y','markerfacecol or',[.6 0 .6],... 'linestyle','-','color','r','linewidth',2);hold offlegend([b,x],'Carrots','Peas','Peppers','Green Beans',...'Cucumbers','Eggplant')
m=400;k=30000;phaser=0.1;A=20;Cratio=[0.1,0.3,0.5,0.8,1]n=length(cratio);t=0:0.01:8;cols=['r','m','g','c','b','y','k'];for nn=1:naratio=-cratio(nn)*sqrt(k/m);wd=sqrt(k/m)*sqrt(1-cratio(nn)^2);z=A*exp(aratio*t).*sin(wd*t+phaser);plot(t,z,cols(nn));jfd(nn)={'阻尼比='};dhd(nn)={num2str(cratio(nn))};hold onendxlabel('時(shí)間');ylabel('幅值')jus=strcat(jfd,dhd);legend(jus(1:n)')grid on;hold off;
??去除legend邊框
??問題描述
??解決方案?
h = legend('x')
set(h,'box','off')
legend boxofft = 0:0.01:2*pi;
figure
plot(t,sin(t))
h = legend('x');
set(h,'box','off')

推薦閱讀
(點(diǎn)擊標(biāo)題可跳轉(zhuǎn)閱讀)
【初學(xué)不要怕】教你全方位理解python函數(shù)及其使用(包括lambda函數(shù)和遞歸函數(shù)詳解系列)
【加解密算法實(shí)現(xiàn)】全面剖析RSA加解密算法(附完整C/Python源碼)
【專家推薦】保姆級(jí)開源工具推薦,一用一個(gè)爽,非常勁爆(收藏系列)
【恭喜考研擬錄取】極力推薦科研必備軟件,讓你科研生涯事半功倍
老鐵,三連支持一下,好嗎?↓↓↓


點(diǎn)分享

點(diǎn)點(diǎn)贊

點(diǎn)在看






















