在两个子图之间画线 [英] Draw line between two subplots

查看:112
本文介绍了在两个子图之间画线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个2×n数组,代表2d点.这两个阵列在同一图中绘制,但是在两个不同的子图中.对于一个阵列中的每个点,在另一个阵列中都有一个对应的点.我想通过从一个子图到另一个子图画一条线来显示这种对应关系.

我找到的解决方案是这样的:

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

这将在轴调用给定的坐标系中绘制一条线.为了使它对我有用,我需要一种在子图系统和新系统之间更改坐标系的方法.有人知道该怎么做吗?

也许有不同的方法可以做到这一点.如果是这样,我很想知道.

解决方案

首先,您必须将轴坐标转换为图形坐标.然后,您可以使用 ANNOTATION 函数在图中绘制线条.

您可以使用数据空间来计算单位转换(ds2nfu)在FileExchange上提交.

这是一个代码示例:

% 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

确保您的数据数组大小相等.

编辑:上面的代码将对当前轴进行数据转换.您也可以针对特定的轴进行此操作:

hAx1 = subplot(211);
% ...
[xa1 ya1] = ds2nfu(hAx1, a1(1,:),a1(2,:));

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.

解决方案

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,:));

这篇关于在两个子图之间画线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆