Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Draw line between two subplots

Tags:

matlab

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, there is a corresponding point i the other array. I want to show this correspondance by drawing a line from one of the subplots to the other subplot.

The solutions i have found are something like:

 ah=axes('position',[.2,.2,.6,.6],'visible','off'); % <- select your pos...
 line([.1,.9],[.1,.9],'parent',ah,'linewidth',5);

This plots a line in the coordinate system given by the axes call. In order for this to work for me i need a way to change coordinate system between the subplots system and the new system. Anybody know how this can be done?

Maybe there is different way of doing this. If so i would love to know.

like image 214
PKeno Avatar asked Sep 03 '10 11:09

PKeno


1 Answers

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,:));
like image 173
yuk Avatar answered Nov 14 '22 13:11

yuk