MATLAB Answers


How to align axes to a point on another figure

Erica McCune さんによって質問されました 2019 年 7 月 19 日 14:52
最新アクティビティ Adam Danz
さんによって コメントされました 2019 年 7 月 19 日 18:35
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,

  0 件のコメント

サインイン to comment.

1 件の回答

Adam Danz
回答者: Adam Danz
2019 年 7 月 19 日 15:49
編集済み: Adam Danz
2019 年 7 月 19 日 18:28

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
C = [0 2 4 6; 8 10 12 14; 16 18 20 22];
hold on
% 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!
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 件のコメント

Adam Danz
2019 年 7 月 19 日 18:27
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!
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 2019 年 7 月 19 日 18:32
Thank you so much, this works perfectly now!
Adam Danz
2019 年 7 月 19 日 18:35
Thanks for pointing out the error! Glad I could help.

サインイン to comment.

Translated by