setting the position of the legend
Afficher commentaires plus anciens
I have code that used to work fine in Matlab 2013 but the new Matlab 2015b does not allow me to set the position of the legend as I wish
Matlab 2015b:

In Matlab 2015b, I can do the following:
a=get(leg,'position')
>>a = 286.0000 414.4667 250.0000 143.0667
set(leg,'position',a/2)
a=get(leg,'position')
>> a = 80.8530 171.4667 249.2941 143.0667
and the figure looks like this:

Actually, I can choose whatever in the 'set(leg,'position',...)' command and, even if I only change the last of the four entries (the height of the legend), the legend will move around a bit in some random direction but it will not resize
Matlab 2013, the way I want it, with a smaller legend size:

Code:
Fig1=figure('units','pixels','position',[0 0 600 600])%,'Visible','off');%figure('Visible','off');
set(0,'defaultlinelinewidth',1.5);
set(0,'defaulttextfontsize',12);
set(gcf, 'Color', 'w');
for j=1:size(NBb,2)
y=1-X(NBb(2,j)).(Plots{i});%output the real values; 1- the probability of not waiting= blocking probability
h(j)=plot(x,y,'color',clrs(j,:),'Marker',Mrkrs{j},'MarkerSize',8);
nummarkers(h(j),5);
%label(h,sprintf('$E[L_{Boarding\\:}\\:]$=%d',NBb(1,j)),'location','right','slope','interpreter','latex');
hold on
d(j)=plot(x,repmat(1-NoBoarding.(Plots{i})(NoBoarding.NbBeds==NoBoarding.NbBeds(1)-NBb(1,j)),1,length(x)),'Marker',Mrkrs{j},'color',clrs(j,:),'linestyle','--','MarkerSize',6);
nummarkers(d(j),5);
end
dummy1=plot(x(1),y(1),'color','k','linestyle','-','visible','off');
dummy2=plot(x(1),y(1),'color','k','linestyle','--','visible','off');
%nob=plot([0,1],repmat(NoBoarding.(Plots{i})(NoBoarding.NbBeds==25),1,2),'color','k','linestyle','--');%a
%line for when there are N beds without boarding
set(gca,'xlim',[0,1]);
if i==4
set(gca,'ylim',[-0.05,max(get(gca,'ylim'))]);
end
set(gca,'xtick',[0:0.1:1]);
set(gca,'xticklabel',[num2cell(0:0.1:1)])
hold off
[leg,icons,plots,legend_text] =legend([h(1),h(2),h(3),h(4),dummy1,dummy2],Legend,'interpreter','latex','fontsize',14);%,'position',[100,100,100,100]);
ypos=[0.875,0.625,0.375,0.125];
xpos=[0.03,0.19];
set(icons(15),'ydata',[ypos(3),ypos(3)]);
set(icons(17),'ydata',[ypos(4),ypos(4)]);
set(icons(5),'position',[0.203,ypos(3),0]);
set(icons(6),'position',[0.203,ypos(4),0]);
for j=1:4
set(icons(5+2*j),'ydata',[ypos(j-2*(j>2)),ypos(j-2*(j>2))]);
set(icons(5+2*j),'xdata',xpos+0.5*(j>2));
set(icons(6+2*j),'ydata',ypos(j-2*(j>2)));
set(icons(6+2*j),'xdata',mean(xpos+0.5*(j>2)));
set(icons(j),'position',[0.203,ypos(j-2*(j>2)),0]+[0.5,0,0]*(j>2))
end
set(leg,'units','pixels');
set(leg,'position',legendpos(i,:))
if Title
title('$Pr_{e,q}(delay)$','interpreter','latex','fontsize',16);
end
xlab=xlabel('$p_b$ (and $\beta= \frac{p_b\lambda}{E[L_{Boarding\:\:}\:\:]})$','interpreter','latex','fontsize',16);
ylab=ylabel('$Pr_{e,q}(delay)$','interpreter','latex','fontsize',16);
1 commentaire
Raïsa Carmen
le 11 Oct 2016
Modifié(e) : Raïsa Carmen
le 11 Oct 2016
Réponses (2)
Massimo Zanetti
le 7 Oct 2016
Modifié(e) : Massimo Zanetti
le 7 Oct 2016
I suggest you to let matlab finding the best position by doing
set(leg,'location','best')
4 commentaires
Raïsa Carmen
le 11 Oct 2016
Massimo Zanetti
le 11 Oct 2016
Usually, similar problems are solved by setting legend 'Units' to 'Normalized' and usind therefore normalized coordinates (in [0,1]).
Let me know if it works.
Raïsa Carmen
le 11 Oct 2016
Davide Cerra
le 26 Août 2019
Grazie
Thorsten
le 7 Oct 2016
The height of the box is the fourth parameter of the position. To reduce the size you can do the following:
p = leg.Position;
p(4)= 0.5*p(4);
leg.Position = p;
However, in the simple example that I tried, the height and width was already as tight as possible, so I managed only to increase width and height, but not to decrease it.
1 commentaire
Raïsa Carmen
le 11 Oct 2016
Modifié(e) : Raïsa Carmen
le 11 Oct 2016
Catégories
En savoir plus sur Legend dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!