如何在Matlab中绘制箭头?

use*_*129 23 matlab plot matlab-figure

我试图在matlab图中绘制一个箭头,没有任何成功.

代码示例:

function [ output_args ] = example( input_args )

figure ('Name', 'example');
x = [10 30]
y = [10 30]
xlim([1, 100])
ylim([1, 100])
arrow (x, y) ???
end
Run Code Online (Sandbox Code Playgroud)

matlab中是否有任何可以绘制箭头的功能?谢谢

the*_*alk 46

你可能会滥用quiver,这样你就不必通过使用来处理非常规的标准化数字单位annotation

drawArrow = @(x,y) quiver( x(1),y(1),x(2)-x(1),y(2)-y(1),0 )    

x1 = [10 30];
y1 = [10 30];

drawArrow(x1,y1); hold on

x2 = [25 15];
y2 = [15 25];

drawArrow(x2,y2)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

重要的是第5个参数quiver:0禁用其他默认缩放,因为此函数实际上用于绘制矢量字段.(或使用poperty值对'AutoScale','off')

您还可以添加其他功能:

drawArrow = @(x,y,varargin) quiver( x(1),y(1),x(2)-x(1),y(2)-y(1),0, varargin{:} )       
drawArrow(x1,y1); hold on
drawArrow(x2,y2,'linewidth',3,'color','r')
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

如果你不喜欢箭头,你需要回到注释,这个答案可能会有所帮助:

如何在箭袋图中更改箭头样式?


关于评论的一些评论:

箭头大小可以根据'MaxHeadSize'属性进行调整,不幸的是它不一致.之后需要设置轴限制

x1 = [10 30];
y1 = [10 30];
drawArrow(x1,y1,{'MaxHeadSize',0.8,'Color','b','LineWidth',3}); hold on

x2 = [25 15];
y2 = [15 25];
drawArrow(x2,y2,{'MaxHeadSize',10,'Color','r','LineWidth',3}); hold on

xlim([1, 100])
ylim([1, 100])
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


sed的解决方案似乎是最好的,因为它提供可调节的箭头.

我只是将它包装成一个函数:

function [ h ] = drawArrow( x,y,xlimits,ylimits,props )

xlim(xlimits)
ylim(ylimits)

h = annotation('arrow');
set(h,'parent', gca, ...
    'position', [x(1),y(1),x(2)-x(1),y(2)-y(1)], ...
    'HeadLength', 10, 'HeadWidth', 10, 'HeadStyle', 'cback1', ...
    props{:} );

end
Run Code Online (Sandbox Code Playgroud)

您可以从脚本中调用,如下所示:

drawArrow(x1,y1,[1, 100],[1, 100],{'Color','b','LineWidth',3}); hold on
drawArrow(x2,y2,[1, 100],[1, 100],{'Color','r','LineWidth',3}); hold on
Run Code Online (Sandbox Code Playgroud)

给你非常相似的结果:

在此输入图像描述


bla*_*bla 8

您可以arrow从文件交换中使用.arrow(Start,Stop)使用从开始到停止的箭头绘制一条线(点应为长度为2或3的向量,或具有2或3列的矩阵),并返回箭头的图形句柄.

编辑: @Lama也是对的,你可以使用,annotation但你需要考虑情节限制.

annotation('arrow',x,y)
Run Code Online (Sandbox Code Playgroud)

创建一个箭头注释对象,该对象从x(1),y(1)定义的点延伸到由x(2),y(2)定义的点,以标准化的图形单位指定.您可以使用 数据空间来计算文件交换中的单位转换函数(ds2nfu.m),以使您的生活更轻松.

[xf yf]=ds2nfu(x,y);
annotation(gcf,'arrow', xf,yf)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

请注意,如果需要,有一些未记录的功能允许将注释固定到图形,请在此处阅读更多相关内容...


Leo*_*yne 7

您可以使用(记录完备的)DaVinci Draw工具箱(完全披露:我写/出售工具箱,虽然箭头是免费的).示例语法和示例输出如下.

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

在此输入图像描述


mar*_*sei 6

在其他解决方案中,这里有一个使用annotation,您可以在其中设置箭头属性,包括(x,y,width,height) 当前轴,头部和线属性.

h=annotation('arrow');
set(h,'parent', gca, ...
    'position', [50 5 20 2], ...
    'HeadLength', 1000, 'HeadWidth', 100, 'HeadStyle', 'hypocycloid', ...
    'Color', [0.4 0.1 0.8], 'LineWidth', 3);
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述