## How to align axes to a point on another figure

### Erica McCune (view profile)

on 19 Jul 2019
Latest activity Commented on by Adam Danz

on 19 Jul 2019

Hi,
I have two imagesc figures that I would like to overlay. One disaplays ultrasound data and the other displays mapping results for a subsection of that ultrasound window. What I would like to do is align the smaller imagesc window on top of the larger ultrasound image, but I need to align the position of the smaller image to the correct location on the ultrasound image. I have created two sets of axes, one for each image so that each image can have its own colormap; however, I am struggling to find a way to set the position of the second axes so that the smaller image is aligned to the correct data point on the first image. Is there a way to get the axes position of a specific data point in the first image so that I can use the axes('position',...) command to correctly align the two images?
Thanks for any help,
Erica

### Release

R2018b

on 19 Jul 2019

on 19 Jul 2019

The problem is that axis positions are in figure space (relative to lower left corner of the figure) while the data point coordinates are in data space (relative to the origin). First you should convert your data point from data-space to figure space which will reqiure 1) knowing the data point's (x,y) coordinates, 2) knowing the x and y axis limits, 3) knowing the position of the axis in the figure.
% Example data
figure()
subplot(2,1,2)
C = [0 2 4 6; 8 10 12 14; 16 18 20 22];
imagesc(C)
hold on
plot(3,2,'kx','markersize',10)
% step 1: get data point's (x,y) coordinate
% step 2: get the x,y axis limits
hold on %we already did this but I included it again b/c it's important!
xl = xlim();
yl = ylim();
% setp 3: get axis position in figure
set(gca, 'Units', 'Normalize')
axpos = get(gca,'position');
% step 4: convert data coordinate from data space to figure space
xycNorm = (xyc - [xl(1),yl(1)])./[range(xl),range(yl)]; %normalized to axis
if strcmpi(get(gca, 'ydir'),'reverse')
xycNorm(2) = 1 - xycNorm(2); % This is needed iff y axis direction is reversed!
end
xycFigNorm = axpos(1:2) + axpos(3:4).*xycNorm; %normalized to figure
% step 5: create new axes
% If you want the axes to be centered on the coordinate, (SHOWN IN EXAMPLE BELOW)
axSize = [0.2, 0.2]; % size of new axes (normalized units)
ax2 = axes('Units','Normalize','Position',[xycFigNorm - axSize./2, axSize], 'color', 'none');
% If you want the new axes' lower-left corner to be aligned with the chosen coordinate
ax2 = axes('Units','Normalize','Position',[xycFigNorm, axSize], 'color', 'none')

Show 1 older comment

on 19 Jul 2019
Sorry Erica , I fixed a small mistake in my answer. My previous answer did not account for the fact that the y axis in imagesc() is reversed. The mistake didn't show in my example because the y coordinate was centered in the axis. I added the following conditional to my answer and the problem is fixed.
if strcmpi(get(gca, 'ydir'),'reverse')
xycNorm(2) = 1 - xycNorm(2); % This is needed iff y axis direction is reversed!
end
Also, when you implement this into your code, it would be wise to replace all of the gca() with the handles to your axes.
Erica McCune

### Erica McCune (view profile)

on 19 Jul 2019
Thank you so much, this works perfectly now!