Adjust legend position outside the figure layout

10 views (last 30 days)
I want to positioned the legend outside in the center of the figure. I have tried the northoutside, northeastoutside and posiiton but didn't get the accurate results. I need your help to resolve this problem.
figure('name','Linearly polarization')
t = tiledlayout(1,4,'TileSpacing','Compact','Padding','Compact');
%Tile1
nexttile
hold on
plot(z,iLpara11,'-k','LineWidth',1.5);
plot(z,iLpara12,'-b','LineWidth',1.5);
plot(z,iLpara13,'-r','LineWidth',1.5);
plot(z,iLpara14,'-g','LineWidth',1.5);
hold on
%title('\fontname{Arial}Homogeneous water cloud');
legend('Case1','Case2','Case3','Case4','position',[.1 .1 .2 .2],'orientation','horizontal','Fontsize',11);
legend boxon
%set(gca,'yscale','log');
xlabel('\fontname{Arial}Height of the cloud(m)');
ylabel('\fontname{Arial}Backscattered signal(unit)');
set(gca,'color','w','Fontsize',10.5,'LineWidth',1,'Fontweight','normal');
set(gca,'box','on','boxstyle','full','Fontname','Arial','Fontsmoothing','on');
set(gca,'xlim',[2005 2155],'xtick',[2005:50:2155],'ylim',[0.*10.^-8 1.4.*10.^-8],'ytick',[0:.35:1.4].*10.^-8);
set(gca,'xgrid','on','ygrid','on','gridcolor','k');
%txt=text(2050,1.3.*10.^-8,'Linear polarization','Color','black','FontSize',11,'FontWeight','normal','FontName','Arial')
nIDs = 1;
alphabet = ('a':'z').';
chars = num2cell(alphabet(1:nIDs));
chars = chars.';
charlbl = strcat('(',chars,')'); % {'(a)','(b)','(c)','(d)'}
text(0.05,0.95,charlbl{1},'Units','normalized','FontSize',14);
%Tile2
nexttile
hold on
plot(z,iLperp11,'-k','LineWidth',1.5);
plot(z,iLperp12,'-b','LineWidth',1.5);
plot(z,iLperp13,'-r','LineWidth',1.5);
plot(z,iLperp14,'-g','LineWidth',1.5);
hold on
%title('\fontname{Arial}Homogeneous water cloud');
%legend('Case1','Case2','Case3','Case4','location','northoutside','orientation','horizontal','Fontsize',11);
%legend boxon
%set(gca,'yscale','log');
xlabel('\fontname{Arial}Height of the cloud(m)');
ylabel('\fontname{Arial}Backscattered signal(unit)');
set(gca,'color','w','Fontsize',10.5,'LineWidth',1,'Fontweight','normal');
set(gca,'box','on','boxstyle','full','Fontname','Arial','Fontsmoothing','on');
set(gca,'xlim',[2005 2155],'xtick',[2005:50:2155],'ylim',[0.*10.^-9 1.2.*10.^-9],'ytick',[0:0.3:1.2].*10.^-9);
set(gca,'xgrid','on','ygrid','on','gridcolor','k');
%txt=text(2050,1.12.*10.^-9,'Linear polarization','Color','black','FontSize',11,'FontWeight','normal','FontName','Arial')
nIDs = 2;
alphabet = ('a':'z').';
chars = num2cell(alphabet(1:nIDs));
chars = chars.';
charlbl = strcat('(',chars,')'); % {'(a)','(b)','(c)','(d)'}
text(0.05,0.95,charlbl{2},'Units','normalized','FontSize',14);
%Tile3
nexttile
hold on
plot(z,iCperp21,'-k','LineWidth',1.5);
plot(z,iCperp22,'-b','LineWidth',1.5);
plot(z,iCperp23,'-r','LineWidth',1.5);
plot(z,iCperp24,'-g','LineWidth',1.5);
hold on
%title('\fontname{Arial}Homogeneous water cloud');
%legend('Case1','Case2','Case3','Case4','location','northoutside','orientation','horizontal','Fontsize',11);
%legend boxon
%set(gca,'yscale','log');
xlabel('\fontname{Arial}Height of the cloud(m)');
ylabel('\fontname{Arial}Backscattered signal(unit)');
set(gca,'color','w','Fontsize',10.5,'LineWidth',1,'Fontweight','normal');
set(gca,'box','on','boxstyle','full','Fontname','Arial','Fontsmoothing','on');
set(gca,'xlim',[2005 2155],'xtick',[2005:50:2155],'ylim',[0.*10.^-8 1.4.*10.^-8],'ytick',[0:.35:1.4].*10.^-8);
set(gca,'xgrid','on','ygrid','on','gridcolor','k');
%txt=text(2050,1.3.*10.^-8,'Circular polarization','Color','black','FontSize',11,'FontWeight','normal','FontName','Arial')
nIDs = 3;
alphabet = ('a':'z').';
chars = num2cell(alphabet(1:nIDs));
chars = chars.';
charlbl = strcat('(',chars,')'); % {'(a)','(b)','(c)','(d)'}
text(0.05,0.95,charlbl{3},'Units','normalized','FontSize',14);
%Tile4
nexttile
hold on
plot(z,iCpara21,'-k','LineWidth',1.5);
plot(z,iCpara22,'-b','LineWidth',1.5);
plot(z,iCpara23,'-r','LineWidth',1.5);
plot(z,iCpara24,'-g','LineWidth',1.5);
hold on
%title('\fontname{Arial}Homogeneous water cloud');
xlabel('\fontname{Arial}Height of the cloud(m)');
%legend('Case1','Case2','Case3','Case4','location','southeast','FontSize',10);
%legend boxoff
%set(gca,'yscale','log');
xlabel('\fontname{Arial}Height of the cloud(m)');
ylabel('\fontname{Arial}Backscattered signal(unit)');
set(gca,'color','w','Fontsize',10.5,'LineWidth',1,'Fontweight','normal');
set(gca,'box','on','boxstyle','full','Fontname','Arial','Fontsmoothing','on');
set(gca,'xlim',[2005 2155],'xtick',[2005:50:2155],'ylim',[0.*10.^-9 2.5.*10.^-9],'ytick',[0:0.625:2.5].*10.^-9);
set(gca,'xgrid','on','ygrid','on','gridcolor','k');
%txt=text(2050,2.35.*10.^-9,'Circular polarization','Color','black','FontSize',11,'FontWeight','normal','FontName','Arial')
nIDs = 4;
alphabet = ('a':'z').';
chars = num2cell(alphabet(1:nIDs));
chars = chars.';
charlbl = strcat('(',chars,')'); % {'(a)','(b)','(c)','(d)'}
text(0.05,0.95,charlbl{4},'Units','normalized','FontSize',14);
% Enlarge figure to full screen.
%set(gcf, 'units','normalized','outerposition',[.3 .3 .6 .6]);
set(gcf, 'units','normalized','outerposition',[.2 .4 .65 .38]);
print(gcf,'figure.tiff','-dtiff','-r300');

Answers (1)

Adam Danz
Adam Danz on 19 Nov 2021
Since you're using tiledLayout the best option is to specify the layout as shows in this example.
lh =legend(__);
lh.Layout.Tile = 'North'; % <----- relative to tiledlayout
  2 Comments
Wiqas Ahmad
Wiqas Ahmad on 22 Nov 2021
Edited: Wiqas Ahmad on 22 Nov 2021
I have found the answer. I'm using the R2019b in which the legend position is locked. Your answer is feasible with R2020b. Anyway thnak you for your attention.
Adam Danz
Adam Danz on 22 Nov 2021
In future questions, you can fill out the "release" field to indicate which Matlab release you'res using.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!