Legend position according to axes

193 Ansichten (letzte 30 Tage)
Timon Rayis
Timon Rayis am 10 Mär. 2020
Kommentiert: Timon Rayis am 10 Mär. 2020
Hello I want to reposition the legend from the start of axes (not in the middle) like this
Can we do this? Here is my code
x = 1:10; y1 = 4*x; y2 = 3*x + 5; figure(1), plot(x,y1,'b',x,y2,'r')
legend('Line 1','Line 2','Location','NorthOutside','Orientation','horizontal','Box','off')

Antworten (1)

Jakob B. Nielsen
Jakob B. Nielsen am 10 Mär. 2020
You can specify the position of your legend yourself with coordinates, rather than using a predetermined location. First two coordinates are the x and y start, second coordinates are the x-wise length and y-wise height of the legend. For example:
x = 1:10; y1 = 4*x; y2 = 3*x + 5; figure(1), plot(x,y1,'b',x,y2,'r')
legend('Line 1','Line 2','Position',[0.2 0.92 0.15 0.0869]);
legend('Orientation','horizontal')
legend('boxoff')
  4 Kommentare
Jakob B. Nielsen
Jakob B. Nielsen am 10 Mär. 2020
Bearbeitet: Jakob B. Nielsen am 10 Mär. 2020
Weird! It seems the properties changes simply from the maximising of the window. I dont know why that is the case...
if you put in
x = 1:10; y1 = 4*x; y2 = 3*x + 5;
ax=axes;
plot(ax,x,y1,'b',x,y2,'r')
lg=legend('Line 1','Line 2','Position',[0.2 0.92 0.15 0.0869],'Orientation','horizontal','Box','off');
and then inspect lg, it has Position: [0.1250 0.9396 0.3000 0.0476] (which is not even the position we specify)... But then when maximising the window and doing nothing else, lg now has Position: [0.2313 0.9542 0.0875 0.0185].
I dont know... If you need the plot maximised, then plot it maximised to begin with and set the legend position accordingly, I guess?
Timon Rayis
Timon Rayis am 10 Mär. 2020
This was exactly my doubt too. but the quick fix you suggested at the end surely helps. Thanks for that.

Melden Sie sich an, um zu kommentieren.

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by