【问题标题】:How to draw an arrow in Matlab?Matlab中如何画箭头?
【发布时间】:2014-11-01 23:58:26
【问题描述】:

我正在尝试在 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中有没有可以画箭头的函数? 谢谢

【问题讨论】:

  • 使用“annotation('arrow',x,y)”时出现错误...?
  • 你是如何使用它的?你输入什么?反正下面已经有2个答案了...
  • 对于出版质量的图形,我的解决方案是遗憾地放弃与 Matlab 的斗争,导出为 EPS,并在需要箭头和精确文本之类的东西时使用 Adob​​e Illustrator。这很糟糕,但我更开心了,结果数字看起来好多了。

标签: matlab plot matlab-figure


【解决方案1】:

你也可以使用例子

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

See illustration

【讨论】:

    【解决方案2】:

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

    重要的是 quiver5th 参数:0 禁用其他默认缩放,因为此函数实际上用于绘制矢量场。 (或使用属性值对'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')
    

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

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


    关于 cmets 的一些说明:

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


    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
    

    给你非常相似的结果:

    【讨论】:

    • 谢谢,但是如果我想查看图表限制 (1..100) 是否有可能?
    • 一如既往地添加xlim([1, 100]); ylim([1, 100])
    • 如果你在drawarrow之后写xlim,ylim就可以了。唯一的问题是箭头在我看来不够大……
    • @natan 您可以通过'MaxHeadSize' 调整它,请参阅编辑。
    • 是的,但关键是您需要为每个坐标和轴限制选择使用适当的尺寸。如果你有 [1 10] 的限制和 [3 8] 的箭头,你会得到巨大的箭头......所以这很好用,但你还需要在解决方案中添加自动一些箭头估计器...... . 类似arrowsize=0.15*abs(diff(xlim).^2+dif(Ylim).^2)
    【解决方案3】:

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

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

    【讨论】:

      【解决方案4】:

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

      给予

      【讨论】:

      • +1 在我看来最好的非文件交换解决方案。我对你的想法进行了一些拉皮条,以使其更加通用。
      【解决方案5】:

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

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

      annotation('arrow',x,y)
      

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

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

      请注意,有一些未记录的功能允许在需要时将注释固定到图形上,请阅读更多相关信息 here...

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-09-17
        • 2014-11-11
        • 2018-03-21
        • 2015-09-28
        • 1970-01-01
        • 2017-05-23
        • 2020-09-07
        相关资源
        最近更新 更多