setting the position of the legend (2024)

63 Ansichten (letzte 30 Tage)

Ältere Kommentare anzeigen

Raïsa Carmen am 7 Okt. 2016

  • Verknüpfen

    Direkter Link zu dieser Frage

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

  • Verknüpfen

    Direkter Link zu dieser Frage

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

Kommentiert: Davide Cerra am 26 Aug. 2019

In MATLAB Online öffnen

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 Kommentar

-1 ältere Kommentare anzeigen-1 ältere Kommentare ausblenden

Raïsa Carmen am 11 Okt. 2016

Direkter Link zu diesem Kommentar

  • Verknüpfen

    Direkter Link zu diesem Kommentar

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

Bearbeitet: Raïsa Carmen am 11 Okt. 2016

In MATLAB Online öffnen

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.

Melden Sie sich an, um diese Frage zu beantworten.

Antworten (2)

Massimo Zanetti am 7 Okt. 2016

  • Verknüpfen

    Direkter Link zu dieser Antwort

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

  • Verknüpfen

    Direkter Link zu dieser Antwort

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

Bearbeitet: Massimo Zanetti am 7 Okt. 2016

In MATLAB Online öffnen

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

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

4 Kommentare

2 ältere Kommentare anzeigen2 ältere Kommentare ausblenden

Raïsa Carmen am 11 Okt. 2016

Direkter Link zu diesem Kommentar

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

  • Verknüpfen

    Direkter Link zu diesem Kommentar

    https://de.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 am 11 Okt. 2016

Direkter Link zu diesem Kommentar

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

  • Verknüpfen

    Direkter Link zu diesem Kommentar

    https://de.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 am 11 Okt. 2016

Direkter Link zu diesem Kommentar

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

  • Verknüpfen

    Direkter Link zu diesem Kommentar

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

In MATLAB Online öffnen

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 am 26 Aug. 2019

Direkter Link zu diesem Kommentar

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

  • Verknüpfen

    Direkter Link zu diesem Kommentar

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

Grazie

Melden Sie sich an, um zu kommentieren.

Thorsten am 7 Okt. 2016

  • Verknüpfen

    Direkter Link zu dieser Antwort

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

  • Verknüpfen

    Direkter Link zu dieser Antwort

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

In MATLAB Online öffnen

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

-1 ältere Kommentare anzeigen-1 ältere Kommentare ausblenden

Raïsa Carmen am 11 Okt. 2016

Direkter Link zu diesem Kommentar

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

  • Verknüpfen

    Direkter Link zu diesem Kommentar

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

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.

Melden Sie sich an, um diese Frage zu beantworten.

Siehe auch

Kategorien

MATLABGraphicsFormatting and AnnotationLabels and AnnotationsLegend

Mehr zu Legend finden Sie in Help Center und 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!

Es ist ein Fehler aufgetreten

Da Änderungen an der Seite vorgenommen wurden, kann diese Aktion nicht abgeschlossen werden. Laden Sie die Seite neu, um sie im aktualisierten Zustand anzuzeigen.


Translated by setting the position of the legend (13)

setting the position of the legend (14)

Website auswählen

Wählen Sie eine Website aus, um übersetzte Inhalte (sofern verfügbar) sowie lokale Veranstaltungen und Angebote anzuzeigen. Auf der Grundlage Ihres Standorts empfehlen wir Ihnen die folgende Auswahl: .

Sie können auch eine Website aus der folgenden Liste auswählen:

Amerika

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

Europa

  • 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)

Asien-Pazifik

Kontakt zu Ihrer lokalen Niederlassung

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

Author: Stevie Stamm

Last Updated:

Views: 5937

Rating: 5 / 5 (60 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Stevie Stamm

Birthday: 1996-06-22

Address: Apt. 419 4200 Sipes Estate, East Delmerview, WY 05617

Phone: +342332224300

Job: Future Advertising Analyst

Hobby: Leather crafting, Puzzles, Leather crafting, scrapbook, Urban exploration, Cabaret, Skateboarding

Introduction: My name is Stevie Stamm, I am a colorful, sparkling, splendid, vast, open, hilarious, tender person who loves writing and wants to share my knowledge and understanding with you.