MATLAB文本框在旋转3D绘图的顶部处于恒定位置?

Mou*_*rew 7 3d matlab text location figure

我试图在MATLAB中的旋转图上有一个文本框,但我不希望文本框改变它相对于图形的位置.我认为'units','normalized'text函数中会这样做,但它并不是很有效,如下例所示.我想我可以使用,uicontrol但我想使用希腊字母,我不能uicontrol看起来那么好text.以下示例重新创建了我的问题.您会注意到文本框会随着情节的旋转而移动,但我希望它只是停留在它开始的左上角区域.谢谢!

part_x = rand(1000,3)-.5;                         %generate random 3D coordinates to scatter
fig1 = figure;
scatter3(part_x(:,1), part_x(:,2), part_x(:,3))
axis equal vis3d
axis([-1 1 -1 1 -1 1])
set(fig1,'color','w')

for tau = 1:150
    view(tau+20,30);                              %spin the plot
    pause(.01)
    if tau~=1; delete(tau_text); end;             %delete the previous text if it exists
    tau_text = text(.1,.7,...
                    ['\tau = ',num2str(tau)],...
                    'units','normalized',...      %text coordinates relative to figure?
                    'Margin',3,...                %these last 3 lines make it look nice
                    'edgecolor','k',...
                    'backgroundcolor','w');
end
Run Code Online (Sandbox Code Playgroud)

Dev*_*-iL 3

几件事:

1)正如您所发现的 - 使用annotation对象而不是text对象是正确的方法。这里很好地解释了差异。

2)您应该只创建annotation一次,然后修改其字符串,而不是在每次迭代时删除并重新创建它。

最后:

part_x = rand(1000,3)-.5;
fig1 = figure;
scatter3(part_x(:,1), part_x(:,2), part_x(:,3))
axis equal vis3d
axis([-1 1 -1 1 -1 1])
set(fig1,'color','w')

%// Create the text outside the loop:
tau_text = annotation('textbox',[0.2 0.8 0.1 0.05],...
                  'string','\tau = NaN',...
                  'Margin',4,... 
                  'edgecolor','k',...
                  'backgroundcolor','w',...
                  'LineWidth',1);

for tau = 1:150
    view(tau+20,30); 
    pause(.01)
    set(tau_text,'String',['\tau = ',num2str(tau)]); %// Modify the string
end
Run Code Online (Sandbox Code Playgroud)

笔记:

1)有趣的是,@Otto建议使用legend结果创建轴因为这就是legend对象 - 带有axes子对象的对象annotation)。然后,您可以手动定位图例,并使用get(gco,'position')(假设它是您最后单击的)或更一般的方法获取其位置get(findobj('tag','legend'),'position')。之后,每当您创建图例时,您都可以将其设置position为您之前获得的图例。您还可以通过从 中删除适当的child类型来删除图例中的线\标记,例如:linelegend

ezplot('sin(x)');
hLeg = legend('\tauex\tau');
delete(findobj(findobj('Tag','legend'),'Type','line'));
hA1 = findobj(findobj('Tag','legend'),'Type','text');
set(hA1,'Position',[0.5,0.5,0],'HorizontalAlignment','center');
Run Code Online (Sandbox Code Playgroud)

当然也可以直接String使用图例的句柄 ( hA1) 来操作图例。

2) UndocumentedMatlab 上的这篇文章讨论了对象的行为annotation以及一些未记录的操作它们的方法。