How to plot arrow with data coordinates in Matlab?

后端 未结 8 1888
没有蜡笔的小新
没有蜡笔的小新 2020-12-13 19:37

I know there is a function named annotation can plot arrows or double arrows. But annotation can only plot in normalized unit. For example:

annotation(\'arro         


        
相关标签:
8条回答
  • 2020-12-13 20:26

    After creating the annotation object you should set the property Units to an absolute one. Example:

    arrowObj = annotation('arrow', [0.1 0.1], [0.5 0.5]);
    set(arrowObj, 'Units', 'centimeters');
    set(arrowObj, 'Position', [1 1 3 5]);
    
    0 讨论(0)
  • 2020-12-13 20:35

    If I remember correctly you need to calculate the position of the axes in relation to the figure.

    it should go like:

    %% 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');
    

    Note Fixed offset in original calculation - ap(3),ap(4) are width and height of gca, not corner positions

    0 讨论(0)
提交回复
热议问题