代码之家  ›  专栏  ›  技术社区  ›  user3668129

如何在Matlab中绘制箭头?

  •  27
  • user3668129  · 技术社区  · 10 年前

    我试图在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
    

    matlab中有什么函数可以画箭头吗? 谢谢

    5 回复  |  直到 9 年前
        1
  •  60
  •   Community CDub    7 年前

    你可能会滥用 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)
    

    enter image description here

    重要的是 第五 论点 颤抖 : 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')
    

    enter image description here

    如果您不喜欢箭头,则需要返回注释,此答案可能会有所帮助:

    How do I change the arrow head style in quiver plot?


    关于评论的一些意见:

    箭头大小可通过 '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])
    

    enter image description here


    The solution by 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
    

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

    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
    

    给出了非常相似的结果:

    enter image description here

        2
  •  9
  •   bla    10 年前

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

    编辑: @喇嘛也是对的,你可以用 annotation 但你需要考虑到地块的限制。

    annotation('arrow',x,y)
    

    创建一个箭头注释对象,该对象从由x(1),y(1)定义的点延伸到由x(2),y 归一化图形单位 。您可以使用 Data space to figure units conversion 函数(ds2nfu.m),使您的生活更轻松。

    [xf yf]=ds2nfu(x,y);
    annotation(gcf,'arrow', xf,yf)
    

    enter image description here

    注意,有一些未记录的特性允许将注释固定到图上,如果需要,请阅读更多信息 here ...

        3
  •  7
  •   marsei    10 年前

    在其他解决方案中,这里有一个使用 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);
    

    给予

    enter image description here

        4
  •  6
  •   Leonard Wayne    9 年前

    您可以使用(有据可查) DaVinci Draw toolbox (完整披露:我写/卖工具箱,尽管箭头是免费的)。示例语法和示例输出如下。

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

    enter image description here

        5
  •  2
  •   Flo    8 年前

    您也可以使用例如

    text(x,y,'\leftarrow t_1','FontSize',12,'FontWeight','bold')
    

    See illustration