Position of scatter points problem

I have a set of scatter points (A), with unit of 'meter'. Then I plot them using:
f = figure(1)
scatter(A(:,1), A(:,2), 3, 'k', 'filled')
but what I noticed is that, the default Units of a figure is 'Pixel', so my question is: is there anyway to know what are the positions of these scatter points in figure?
Thanks!
Yu

2 Comments

Why do you care? As you resize the figure, the pixel location will change, however the coordinates read off the x and y axes will not change, and that's what counts. If you have a pint at (1,4) then it might be at pixel (400, 24) but if you resize the figure, it might be at pixel (135, 395) but that does not matter. The scatter plot will still show the point at (1,4) if you read the values off the x and y axes. So tell me why it matters to you what the pixel coordinates are.
just want know why and how, this is a interesting thing in Matlab I think.
a simple example is that:
x=0;
y=0;
r=1;
th = 0:pi/50:2*pi;
xunit = r * cos(th) + x;
yunit = r * sin(th) + y;

Sign in to comment.

 Accepted Answer

https://www.mathworks.com/help/images/ref/axes2pix.html can give you pixels relative to the axes position. You can
ax = gca;
oldunits = get(ax, 'Units');
set(ax, 'Units', 'pixels');
axpos = get(ax, 'Position');
set(ax, 'Units', oldunits);
and now axpos will be in pixels relative to the figure, to which you could add the results of axes2pix.
I seem to remember there being a more direct internal routine, but I do not remember the name of it.

5 Comments

Hi:
Thanks for your reply. seems this is for image, when I use this for my situation:
x=0;
y=0;
r=1;
th = 0:pi/50:2*pi;
xunit = r * cos(th) + x;
yunit = r * sin(th) + y;
f=figure(1);
scatter(xunit,yunit,2,'k','filled')
the get(f,'CData') reports error.
is there any mistake with my operation?
Thanks!
Yu
After the scatter()
ax = gca;
oldunits = get(ax, 'Units');
set(ax, 'Units', 'pixels');
axpos = get(ax, 'Position');
set(ax, 'Units', oldunits);
XL = xlim(ax);
YL = ylim(ax);
xpos_pixel = (xunit - XL(1)) ./ (XL(2) - XL(1)) * axpos(3) + axpos(1);
ypos_pixel = (yunit - YL(1)) ./ (YL(2) - YL(1)) * axpos(4) + axpos(2);
Thanks for your reply. this works.
The only thing is that, there may be a typo in xpos_pixel equation:
xpos_pixel = (xunit - XL(1)) ./ (XL(2) - XL(1)) * axpos(2) + axpos(1)
which may be:
xpos_pixel = (xunit - XL(1)) ./ (XL(2) - XL(1)) * axpos(3) + axpos(1)
please revise this and I will accept your answer then.
Bests,
Yu
You are right. Fixed.
Thank you!

Sign in to comment.

More Answers (1)

David López
David López on 8 Mar 2022
Hi,
I am trying to select data in a scatter plot with the function impoly, then what I try to do is extract the original matrix position of the data. I don´t know if you know how to do this or if it is possible.
I try with this code but i cant obtain the matrix position in the original matrix.
ax = gca;
oldunits = get(ax, 'Units');
set(ax, 'Units', 'pixels');
axpos = get(ax, 'Position');
set(ax, 'Units', oldunits);
XL = xlim(ax);
YL = ylim(ax);
xpos_pixel = (xunit - XL(1)) ./ (XL(2) - XL(1)) * axpos(3) + axpos(1);
ypos_pixel = (yunit - YL(1)) ./ (YL(2) - YL(1)) * axpos(4) + axpos(2);
Thank you.
David

Community Treasure Hunt

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

Start Hunting!