필터 지우기
필터 지우기

How to align axes to a point on another figure

조회 수: 17 (최근 30일)
Erica McCune
Erica McCune 2019년 7월 19일
댓글: Adam Danz 2019년 7월 19일
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

채택된 답변

Adam Danz
Adam Danz 2019년 7월 19일
편집: Adam Danz 2019년 7월 19일
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
Erica McCune
Erica McCune 2019년 7월 19일
Thank you so much, this works perfectly now!
Adam Danz
Adam Danz 2019년 7월 19일
Thanks for pointing out the error! Glad I could help.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Image Processing and Computer Vision에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by