MATLAB:如何获得相对于图形位置而不是图形轴的单击值
作为背景,我试图做的是在当前绘图中添加一个插图。我已经完成了大部分工作。下面是一个代码示例:MATLAB:如何获得相对于图形位置而不是图形轴的单击值,matlab,user-controls,matlab-figure,axis,ginput,Matlab,User Controls,Matlab Figure,Axis,Ginput,作为背景,我试图做的是在当前绘图中添加一个插图。我已经完成了大部分工作。下面是一个代码示例: h = figure(1); %Arbitrary figure #1 plot(990000:1000000,990000:1000000,'--r'); g = figure(2); %Arbitary figure #2 plot(randn(100,1),randn(100,1),'.k'); figure(3); %Figure to combine the above two figur
h = figure(1); %Arbitrary figure #1
plot(990000:1000000,990000:1000000,'--r');
g = figure(2); %Arbitary figure #2
plot(randn(100,1),randn(100,1),'.k');
figure(3); %Figure to combine the above two figures
new_fig=gcf;
main_fig = findobj(h,'Type','axes');
h_main = copyobj(main_fig,new_fig);
set(h_main,'Position',get(main_fig,'Position'))
inset_fig = findobj(g,'Type','axes');
h_inset = copyobj(inset_fig,new_fig);
ax=get(main_fig,'Position');
inset_size = 0.3;
X = 0.2; %Left position of inset hard-coded in
Y = 0.6; %Bottom position of inset hard-coded in
set(h_inset,'Position', [X Y inset_size inset_size])
close(h); close(g);
h = figure(1); %Arbitrary figure #1
plot(990000:1000000,990000:1000000,'--r');
[x,y] = ginput(1);
在上面的代码示例中,我只是将插入图的X和Y位置手动设置为X=0.2和Y=0.6
然而,我一直坚持的棘手的部分是,我希望X和Y位置由用户确定。我希望用户能够在某处点击图形,点击的点成为插图的中心点
不幸的是,ginput的工作方式与我希望的不完全一样,因为[x,y]=ginput(1)返回的值x和y是相对于图形轴的。下面是一个代码示例:
h = figure(1); %Arbitrary figure #1
plot(990000:1000000,990000:1000000,'--r');
g = figure(2); %Arbitary figure #2
plot(randn(100,1),randn(100,1),'.k');
figure(3); %Figure to combine the above two figures
new_fig=gcf;
main_fig = findobj(h,'Type','axes');
h_main = copyobj(main_fig,new_fig);
set(h_main,'Position',get(main_fig,'Position'))
inset_fig = findobj(g,'Type','axes');
h_inset = copyobj(inset_fig,new_fig);
ax=get(main_fig,'Position');
inset_size = 0.3;
X = 0.2; %Left position of inset hard-coded in
Y = 0.6; %Bottom position of inset hard-coded in
set(h_inset,'Position', [X Y inset_size inset_size])
close(h); close(g);
h = figure(1); %Arbitrary figure #1
plot(990000:1000000,990000:1000000,'--r');
[x,y] = ginput(1);
如您所见,x和y的数量级为10^5,因为它们参照的是打印轴。因此,ginput
中的(x,y)与设置地物位置的(x,y)不匹配
如何将(x,y)转换为(x,y)?
编辑:我尝试了以下缩放,它“几乎”起作用,但不完全起作用,任何关于如何改进的想法都值得赞赏:
h = figure(1); %Arbitrary figure #1
plot(990000:1000000,990000:1000000,'--r');
[x,y] = ginput(1);
limx = xlim;
limy = ylim;
g = figure(2); %Arbitary figure #2
plot(randn(100,1),randn(100,1),'.k');
figure(3); %Figure to combine the above two figures
new_fig=gcf;
main_fig = findobj(h,'Type','axes');
h_main = copyobj(main_fig,new_fig);
set(h_main,'Position',get(main_fig,'Position'))
inset_fig = findobj(g,'Type','axes');
h_inset = copyobj(inset_fig,new_fig);
ax=get(main_fig,'Position');
inset_size = 0.3;
% X = 0.2; %Left position of inset hard-coded in
% Y = 0.6; %Bottom position of inset hard-coded in
%CONVERT ginput (x,y) to axis position (X,Y)
X = (x-min(limx))/diff(limx)*ax(3);
Y = (y-min(limy))/diff(limy)*ax(4);
set(h_inset,'Position', [X Y inset_size inset_size])
close(h); close(g);
我将使用一个小函数将单位从“标准化”转换为“数据”:
ax2norm = @(x,y,ax) [...
ax.Position(3)*((x-ax.XLim(1))./range(ax.XLim))+ ax.Position(1)...
ax.Position(4)*((y-ax.YLim(1))./range(ax.YLim))+ ax.Position(2)];
% white area * ((value - axis min) / axis length) + gray area
其中,x
和y
是标准化坐标,ax
是转换为其数据单位的轴句柄
下面的示例与您想要实现的类似,但更为简洁和一般:
% create the inset:
insetFig = figure('Visible','off');
plot(randn(100,2),'.k')
temp_ax = gca;
% create the main figure:
mainFig = figure;
plot(1:10,1:10,'--r')
main_ax = gca;
% get position for inset:
inset_size = 0.3;
[x,y] = ginput(1);
XY = ax2norm(x,y,main_ax);
% copy the inset to the position:
inset_ax = copyobj(temp_ax,mainFig);
close(insetFig);
set(inset_ax,'Position', [XY-inset_size/2 inset_size inset_size])
具体而言,以下是主要变化:
gca
获取轴句柄,因为它比findobj
其中一些更改可能不适合您的情况,但对于示例的运行来说,没有一个是至关重要的。答案的实质是上面的函数。什么是“几乎有效”呢?如果您尝试上面的代码示例,您将看到插入被添加到绘图中,但插入的中心与用户单击的位置不一致。我希望插入的中心正好添加到用户单击的位置。我上面的例子把它放在附近的某个地方,但它似乎取决于数字的大小和其他因素。