Draw line between two subplots

后端 未结 2 494
梦如初夏
梦如初夏 2020-12-06 07:17

I have two two-by-n arrays, representing 2d-points. These two arrays are plotted in the same figure, but in two different subplots. For every point in one of the arrays, the

相关标签:
2条回答
  • 2020-12-06 07:34

    First you have to convert axes coordinates to figure coordinates. Then you can use ANNOTATION function to draw lines in the figure.

    You can use Data space to figure units conversion (ds2nfu) submission on FileExchange.

    Here is a code example:

    % two 2x5 arrays with random data
    a1 = rand(2,5);
    a2 = rand(2,5);
    
    % two subplots
    subplot(211)
    scatter(a1(1,:),a1(2,:))
    % Convert axes coordinates to figure coordinates for 1st axes
    [xa1 ya1] = ds2nfu(a1(1,:),a1(2,:));
    
    
    subplot(212)
    scatter(a2(1,:),a2(2,:))
    % Convert axes coordinates to figure coordinates for 2nd axes
    [xa2 ya2] = ds2nfu(a2(1,:),a2(2,:));
    
    % draw the lines
    for k=1:numel(xa1)
        annotation('line',[xa1(k) xa2(k)],[ya1(k) ya2(k)],'color','r');
    end
    

    Make sure your data arrays are equal in size.

    Edit: The code above will do data conversion for a current axes. You can also do it for particular axes:

    hAx1 = subplot(211);
    % ...
    [xa1 ya1] = ds2nfu(hAx1, a1(1,:),a1(2,:));
    
    0 讨论(0)
  • 2020-12-06 07:40

    A simple solution is to use the toolbar in the figure window. Just click "insert" and then "Line".

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