setting the position of the legend (2024)

63 views (last 30 days)

Show older comments

Raïsa Carmen on 7 Oct 2016

  • Link

    Direct link to this question

    https://www.mathworks.com/matlabcentral/answers/306226-setting-the-position-of-the-legend

  • Link

    Direct link to this question

    https://www.mathworks.com/matlabcentral/answers/306226-setting-the-position-of-the-legend

Commented: Davide Cerra on 26 Aug 2019

Open in MATLAB Online

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:

setting the position of the legend (2)

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:

setting the position of the legend (3)

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:

setting the position of the legend (4)

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 Comment

Show -1 older commentsHide -1 older comments

Raïsa Carmen on 11 Oct 2016

Direct link to this comment

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/306226-setting-the-position-of-the-legend#comment_397811

Edited: Raïsa Carmen on 11 Oct 2016

Open in MATLAB Online

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,:))

Sign in to comment.

Sign in to answer this question.

Answers (2)

Massimo Zanetti on 7 Oct 2016

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/306226-setting-the-position-of-the-legend#answer_237958

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/306226-setting-the-position-of-the-legend#answer_237958

Edited: Massimo Zanetti on 7 Oct 2016

Open in MATLAB Online

I suggest you to let matlab finding the best position by doing

set(leg,'location','best')

4 Comments

Show 2 older commentsHide 2 older comments

Raïsa Carmen on 11 Oct 2016

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/306226-setting-the-position-of-the-legend#comment_397800

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/306226-setting-the-position-of-the-legend#comment_397800

But then Matlab puts it somewhere I don't want it to put it. Furthermore, I still cannot resize it. Why can I not just choose myself as I use to be able to do in Matlab 2013?

Massimo Zanetti on 11 Oct 2016

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/306226-setting-the-position-of-the-legend#comment_397825

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/306226-setting-the-position-of-the-legend#comment_397825

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 on 11 Oct 2016

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/306226-setting-the-position-of-the-legend#comment_397847

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/306226-setting-the-position-of-the-legend#comment_397847

Open in MATLAB Online

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 on 26 Aug 2019

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/306226-setting-the-position-of-the-legend#comment_738978

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/306226-setting-the-position-of-the-legend#comment_738978

Grazie

Sign in to comment.

Thorsten on 7 Oct 2016

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/306226-setting-the-position-of-the-legend#answer_237980

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/306226-setting-the-position-of-the-legend#answer_237980

Open in MATLAB Online

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 Comment

Show -1 older commentsHide -1 older comments

Raïsa Carmen on 11 Oct 2016

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/306226-setting-the-position-of-the-legend#comment_397801

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/306226-setting-the-position-of-the-legend#comment_397801

Edited: Raïsa Carmen on 11 Oct 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.

Sign in to comment.

Sign in to answer this question.

See Also

Categories

MATLABGraphicsFormatting and AnnotationLabels and AnnotationsLegend

Find more on Legend in Help Center and File Exchange

Tags

  • legend
  • position
  • resize

Community Treasure Hunt

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

Start Hunting!

An Error Occurred

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.


setting the position of the legend (13)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom(English)

Asia Pacific

Contact your local office

setting the position of the legend (2024)
Top Articles
Latest Posts
Article information

Author: Trent Wehner

Last Updated:

Views: 5929

Rating: 4.6 / 5 (56 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Trent Wehner

Birthday: 1993-03-14

Address: 872 Kevin Squares, New Codyville, AK 01785-0416

Phone: +18698800304764

Job: Senior Farming Developer

Hobby: Paintball, Calligraphy, Hunting, Flying disc, Lapidary, Rafting, Inline skating

Introduction: My name is Trent Wehner, I am a talented, brainy, zealous, light, funny, gleaming, attractive person who loves writing and wants to share my knowledge and understanding with you.