Effacer les filtres
Effacer les filtres

what does the four vector mean in matlab legend 'Position'?

80 vues (au cours des 30 derniers jours)
Martin
Martin le 3 Juil 2013
Commenté : Malek Barhoush le 24 Oct 2020
I use the line:
leg = legend('Ionisation','Recombination');
pos = get(leg,'Position')
But get returned the answer:
pos =0.638988095238095,0.209126984126984, O.257142857142857,0.1
Can I ask what this means because this not the positions on the axes because when you look by eye it seems incorrect...
All I want to do is find the location (on the axes of the graph) of the bottom left corner so I can add another text box below with a string in it...
Thanks in advance MA

Réponses (4)

Azzi Abdelmalek
Azzi Abdelmalek le 3 Juil 2013
pos(1) and pos(2) are the coordinate of the legend windows
pos(3) and pos(4) are width and height of this windows
  3 commentaires
Azzi Abdelmalek
Azzi Abdelmalek le 4 Juil 2013
To understand try:
x0=0;
y0=0;
width=0.25;
height=0.25;
plot(1,1,2,2);
legend({'a','b'},'position',[x0 y0 width height])
change the position x0, and y0 then run again
x0=0.5;
y0=1
legend({'a','b'},'position',[x0 y0 width height])
x0,y0 are the coordinate of the left down corner of the legend windows
Malek Barhoush
Malek Barhoush le 24 Oct 2020
x0=0;
y0=0;
width=0.25;
height=0.25;
plot(1,1,2,2);
legend({'a','b'},'position',[x0 y0 width height])

Connectez-vous pour commenter.


Walter Roberson
Walter Roberson le 4 Juil 2013
In order to understand the coordinates, you need to get() the Units property. Units can be any of several things, but in my experience the three that are used the most are "normal", "pixel", and "data".
Pixel is a measurement in pixels; fractional pixels are allowed.
Normal is based on fractions of the containing object, 0 being none of the object and 1 being the entire object. For example 'normal' and 0.3 as an axis position would mean 3/10 of the way along (from the left) the containing figure (or uipanel)
Data is data coordinates, based around the current xlim, ylim, or zlim. For example in your call
plot(1,1,2,2)
the 1's and 2's are in data coordinates.
If a graphics object has no Units property but has a Position property, then the graphics object is locked into Data coordinates; those kinds of graphics objects must be children of an axes.

Jan
Jan le 4 Juil 2013
Modifié(e) : Jan le 4 Juil 2013
You can learn more about the position by experimenting:
leg = legend('Ionisation','Recombination');
pos = get(leg,'Position');
pause(1);
set(leg, 'Position', [0.64, 0.21, O.25, 0.1]);
pause(1);
set(leg, 'Position', [0.74, 0.21, O.25, 0.1]);
pause(1);
set(leg, 'Position', [0.64, 0.31, O.25, 0.1]);
pause(1);
set(leg, 'Position', [0.64, 0.21, O.35, 0.1]);
pause(1);
set(leg, 'Position', [0.64, 0.21, O.25, 0.2]);
pause(1);
set(leg, 'Position', [0.0, 0.0, 1.0, 1.0]);
pause(1);
set(leg, 'Position', [0.5, 0.5, 0.4, 0.4]);

Martin
Martin le 4 Juil 2013
Thanks for all the information on this guys. I think what I have is the dimensions given in 'normal' units. Basically I have to run this simulation a lot of times, each time the y axis can change by 2 orders of magnitude and therefore I can't hardwire the textbox positions. I'm hoping that I can find some sort of conversion factor between 'normal' and 'data' coords as that's what the textbox positions are given in.
  2 commentaires
Jan
Jan le 4 Juil 2013
Modifié(e) : Jan le 4 Juil 2013
The limits of the axes do not matter, whan you use 'normalized' posiotions. Matlab converts the absolute position on the screen automatically for you. This code writes the string to the upper right corner without knowing the data size:
axes('XLim', cumsum(rand(1, 2)), 'YLim', cumsum(rand(1, 2)));
H = text(0.9, 0.9, 'Hello', 'VerticalAlignment', 'top', ...
'HorizontalAlignment', 'right', ...
'Units', 'normalized');
drawnow;
% Obtain the position in data units if wanted:
set(H, 'Units', 'data');
dataPos = get(H, 'Position');
Martin
Martin le 4 Juil 2013
Ah that's a really good way to do it, thanks. I appreciate that a lot, and certainly have learned a good deal.

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by