How to align axes to a point on another figure

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

 Risposta accettata

Adam Danz
Adam Danz il 19 Lug 2019
Modificato: Adam Danz il 19 Lug 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
xyc = [3,2]; %already known
% 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')

4 Commenti

Thank you for your response! It was very helpful. I am having one problem, however. My code is using an imagesc data set where I define my x and y values in the imagesc declaration. If I modify the code you provided to include that, the axes are no longer located in the correct position. The x location is still in the same desired location but the y is always slightly further up than it shoud be. Do you know why this might be?
clear, clc, close all;
% Example data
figure()
subplot(2,1,2)
x = 0.5:0.5:3.5;
y = 0.5:0.5:4.5;
C = rand(length(y), length(x));
imagesc(x,y,C)
hold on
plot(3,2,'kx','markersize',10)
% step 1: get data point's (x,y) coordinate
xyc = [3,2]; %already known
% 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
xycFigNorm = axpos(1:2) + axpos(3:4).*xycNorm; %normalized to figure
% step 5: create new axes
axSize = [0.2, 0.2]; % size of new axes (normalized units)
% 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')
hold off
axes.jpg
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.
Thank you so much, this works perfectly now!
Thanks for pointing out the error! Glad I could help.

Accedi per commentare.

Più risposte (0)

Prodotti

Release

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by