setting the position of the legend

55 Ansichten (letzte 30 Tage)
Raïsa Carmen
Raïsa Carmen am 7 Okt. 2016
Kommentiert: Davide Cerra am 26 Aug. 2019
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 Kommentar
Raïsa Carmen
Raïsa Carmen am 11 Okt. 2016
Bearbeitet: Raïsa Carmen am 11 Okt. 2016
Here is a working example code. How do I reduce the size of the legend? And why isn't the set(leg, 'position'...) command not working anymore?
Fig1=figure('units','pixels','position',[0 0 600 600])%,'Visible','off');%figure('Visible','off');
Legend={'x=2$\quad$','x=4$\quad$','x=6$\quad$','x=8$\quad$','$E[L_{Boarding}]$=x, N=25','$E[L_{Boarding}]$=0, N=25-x'};
set(0,'defaultlinelinewidth',1.5);
set(0,'defaulttextfontsize',12);
set(gcf, 'Color', 'w');
x=1:10;
y=rand(4,10);
a=repmat(rand(4,1),1,10);
clrs=[0,1,0.4;0,0.6,0.4;0,0.4,0.6;0,0,0.4];
legendpos=[286,200,250,80];
Mrkrs={'o','s','d','*'};
for j=1:4
h(j)=plot(x,y(j,:),'color',clrs(j,:),'Marker',Mrkrs{j},'MarkerSize',8);
hold on
d(j)=plot(x,a(j,:),'Marker',Mrkrs{j},'color',clrs(j,:),'linestyle','--','MarkerSize',6);
end
dummy1=plot(x(1),y(1),'color','k','linestyle','-','visible','off');
dummy2=plot(x(1),y(1),'color','k','linestyle','--','visible','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,:))

Melden Sie sich an, um zu kommentieren.

Antworten (2)

Massimo Zanetti
Massimo Zanetti am 7 Okt. 2016
Bearbeitet: Massimo Zanetti am 7 Okt. 2016
I suggest you to let matlab finding the best position by doing
set(leg,'location','best')
  4 Kommentare
Raïsa Carmen
Raïsa Carmen am 11 Okt. 2016
Thank you for the suggestion but that does not work. As you can see, a similar trick worked in 2013 (I set the units to pixels in my code above). If I substiture the last two lines of the working example code by this
set(leg,'units','normalized');
set(leg,'position',[0.5,0.5,0.1,0.1])
get(leg,'position') will still yield something like [0.3423,0.4308,0.4155,0.2384] so, obviously matlab still did not do what I wanted it to do. The legens size is not reduced
Davide Cerra
Davide Cerra am 26 Aug. 2019
Grazie

Melden Sie sich an, um zu kommentieren.


Thorsten
Thorsten am 7 Okt. 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 Kommentar
Raïsa Carmen
Raïsa Carmen am 11 Okt. 2016
Bearbeitet: Raïsa Carmen am 11 Okt. 2016
Thank you for your answer but as I said earlier, any of these commands, for some reason, do not do what they are supposed to do anymore. I can give any specific array of 4 numbers to matlab to set the position but they are not interpreted correctly and Matlab seems to just randomly change the position of the legend a bit.

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by