如何在Matlab中用数据坐标绘制箭头?

时间:2012-07-16 06:51:14

标签: matlab plot annotations figure

我知道有一个名为annotation的函数可以绘制箭头或双箭头。但是注释只能以标准化单位绘制。例如:

annotation('arrows',[x1 x2],[y1 y2])

此处,[x1,x2]应为小于1的比率数。

所以,我的问题是如何绘制具有真值而不是标准化值的箭头?

我想知道是否有任何其他功能可以接近这个或者是否有任何函数我可以得到图的轴值,以便我可以将真值调整为标准化值。

8 个答案:

答案 0 :(得分:12)

对于注释的定位,Matlab提供函数dsxy2figxy将数据空间点转换为标准化空间坐标。但是,无论出于何种原因,该功能都不包含在Matlab发行版中,必须先“创建”。

将以下行复制到命令窗口并执行它以在编辑器中打开该功能。

edit(fullfile(docroot,'techdoc','creating_plots','examples','dsxy2figxy.m'))

使用函数dsxy2figxy将其保存在matlab搜索路径中的某个位置。

请在matlab-central找到函数dsxy2figxy的完整说明:http://www.mathworks.de/help/techdoc/creating_plots/bquk5ia-1.html

答案 1 :(得分:10)

即使annotation使用normalized作为默认单位,您也可以将这些对象与当前轴(gca)相关联,并使用数据单位设置X和{{ 1}}属性。

以下是绘制单箭头的示例。

Y

enter image description here

答案 2 :(得分:8)

对于遇到此主题的人来说,想要在“数据空间”中绘制箭头而不是相对于图形和/或轴的单位,我强烈推荐文件交换中的arrow.m

答案 3 :(得分:5)

我刚刚发现了这种方法,因为我不想打扰标准化的单位。使用乳胶解释器:

figure
plot([1:5],[1:5]*3,'.-')
%// Say I want to put an arrow pointing to the location, [3 9]    
text(2.94,8.3,'\uparrow','fontsize',20)
text(2.8,7.8,'point [3,9]')

要使箭头更长,请使用更大的字体大小。

<强>赞成

  • 比使用标准化单位更容易,更快捷,更快捷
  • 不需要安装任何功能(对我们懒惰的人有好处。)
  • 使用LaTeX解释器,有一系列箭头(向上,向下,向左,向右和其他角度(见Symbol list

<强>缺点

  • 绝对需要试用和错误/调整才能获得箭头相对于POI的正确位置。
  • 对箭头长度的控制有限
  • 解释器(boo)无法理解某些乳胶命令。

答案 4 :(得分:2)

如果我没记错的话,你需要计算轴相对于图的位置。

它应该像:

%% example plot
clf
plot(rand(5,2)*5)
%% get info specific to the axes you plan to plot into
set(gcf,'Units','normalized')
set(gca,'Units','normalized')
ax = axis;
ap = get(gca,'Position')
%% annotation from 1,2 to 3,4
xo = [1,3];
yo = [2,4];
xp = (xo-ax(1))/(ax(2)-ax(1))*ap(3)+ap(1);
yp = (yo-ax(3))/(ax(4)-ax(3))*ap(4)+ap(2);
ah=annotation('arrow',xp,yp,'Color','r');

注意原始计算中的固定偏移量 - ap(3),ap(4)是gca的宽度和高度,而不是角位置

答案 5 :(得分:1)

创建注释对象后,应将属性 Units 设置为绝对值。例如:

arrowObj = annotation('arrow', [0.1 0.1], [0.5 0.5]);
set(arrowObj, 'Units', 'centimeters');
set(arrowObj, 'Position', [1 1 3 5]);

答案 6 :(得分:0)

一种方法是在轴单位中定义箭头:

Ax=[0 -0.003 0.003 0];       % (Ax,Ay) form an upward pointing arrowhead.
Ay=[0.01 0.0060 0.0060 0.01];
Ax=Ax-mean(Ax);  % center it on zero
Ay=Ay-mean(Ay);

然后在曲线vv上的所需箭头索引处,计算

x1=vv(in,1); y1=vv(in,2);
x2=vv(in+1,1); y2=vv(in+1,2);
u=x2-x1;
v=y2-y1;
th=-pi/2+atan2(v,u);
R=[cos(th) -sin(th); sin(th) cos(th)];   % Rotation matrix for local slope of vv.
A=R*[Ax;Ay];    % Rotate the arrowhead.
patch(x1+A(1,:),y1+A(2,:),'r','LineWidth',0.01)   % plot rotated arrowhead at (x1,y1).
plot(x1+A(1,:),y1+A(2,:),'r','LineWidth',0.01)    % Kludge to make boundary red too (I'm sure there is a more elegant way).

根据我的具体情况为我工作。

答案 7 :(得分:0)

您可以使用(详细记录的)DaVinci Draw toolbox中的“箭头”组件(完全披露:我写了/出售工具箱,虽然箭头是免费的)。

示例语法和示例输出如下。

davinci( 'arrow', 'X', [0 10], 'Y', [0 2], <plus-lots-of-options> )

enter image description here