2017-09-02 111 views
2

我想突出两个阶梯图的相交区域。我能够选择相交区域内的点,并希望使用补丁命令创建填充的形状,但补丁命令无法解决。但是,有些要点仍然必须排除,并且必须添加相交点。如何在楼梯图中突出显示重叠区域?

另一个想法是创建两个区域图,看起来像楼梯用图表:

x = pc_bh(1, :); 
y = pc_bh(2, :); 
x = [x; x]; 
y = [y; y]; 
area(x([2:end end]),y(1:end)) 
hold on; 
x = pc_bh(3, :); 
y = pc_bh(4, :); 
x = [x; x]; 
y = [y; y]; 
area(x([2:end end]),y(1:end)) 

和他们相交,这也不能工作。

这里是理想的结果:

enter image description here

这里是一个与相交区域内的点标记一个情节:

enter image description here

为标记的代码很简单:

pointsA = []; 
pointsB = []; 
lowerLimit = pc_bh(3, 1); 
upperLimit = pc_bh(1, 11); 

for entry=2:11 
    if pc_bh(1, entry) >= lowerLimit && pc_bh(1, entry) <= upperLimit 
     pointsA = vertcat(pointsA, [pc_bh(1, entry), pc_bh(2, entry)]); 
     pointsA = vertcat(pointsA, [pc_bh(1, entry), pc_bh(2, entry) + 1/10]); 
    end 
    if pc_bh(3, entry) >= lowerLimit && pc_bh(3, entry) <= upperLimit 
     pointsB = vertcat(pointsB, [pc_bh(3, entry), pc_bh(4, entry)]); 
      pointsB = vertcat(pointsB, [pc_bh(3, entry), pc_bh(4, entry) - 1/9]); 
    end 
end 
plot(pointsA(:, 1), pointsA(:, 2), 'xr'); 
plot(pointsB(:, 1), pointsB(:, 2), 'xb'); 

数据集是一个4 x 11矩阵,第1行/第2行包含第一个图形的x/y值,第二个图形包含第三个/第四个行的x/y值。

这是所使用的数据集:

0.99754 0.99754 0.99772 0.99790 0.99808 0.99821 0.99842 0.99870 0.99886 0.99900 0.99918 
1  0.9  0.8  0.7  0.6  0.5  0.4  0.3  0.2  0.1  0 
0.99873 0.99873 0.99899 0.99918 0.99928 0.99945 0.99969 0.99973 0.99987 0.99993 0.99993 
0  0.11111 0.22222 0.33333 0.44444 0.55555 0.66666 0.77777 0.88888 1  1 

回答

2

源于这样的事实,每个阶梯的情节在一组不同的x值的评估的难度。本质上,您需要将每个阶梯图的值插入另一个阶梯的相应x值,以便您可以比较相同点处的y值,以查看哪一个最小。 Normal interpolation会遇到问题,因为您必须在阶梯图中重复x值。另一种方法是使用histcounts函数为每个绘图查找其他绘图点的落点。这里有一个函数stairarea,说明这一点,采取两套X和Y数据作为输入,并使用stairs和​​创建一个情节:

function stairarea(x1, y1, x2, y2) 

    % Find overlap of curve 1 on curve 2: 
    [~, ~, index] = histcounts(x1, x2); 
    xi = x1(index > 0); 
    yi = min(y1(index > 0), y2(index(index > 0))); 

    % Find overlap of curve 2 on curve 1: 
    [~, ~, index] = histcounts(x2, x1); 
    xi = [xi x2(index > 0)]; 
    yi = [yi min(y2(index > 0), y1(index(index > 0)))]; 

    % Sort and create stairstep data for overlapping points: 
    [xi, index] = sort(xi); 
    yi = yi(index); 
    [xi, yi] = stairs(xi, yi); 

    % Create plot: 
    area(xi, yi, 'FaceColor', 'y', 'EdgeColor', 'none'); 
    hold on; 
    stairs(x1, y1, 'b'); 
    stairs(x2, y2, 'r'); 

end 

你还可以用您的样本数据使用像这样:

pc_bh = [0.99754 0.99754 0.99772 0.99790 0.99808 0.99821 0.99842 0.99870 0.99886 0.99900 0.99918; ... 
     1  0.9  0.8  0.7  0.6  0.5  0.4  0.3  0.2  0.1  0; ... 
     0.99873 0.99873 0.99899 0.99918 0.99928 0.99945 0.99969 0.99973 0.99987 0.99993 0.99993; ... 
     0  0.11111 0.22222 0.33333 0.44444 0.55555 0.66666 0.77777 0.88888 1  1]; 
stairarea(pc_bh(1, :), pc_bh(2, :), pc_bh(3, :), pc_bh(4, :)); 

,你会得到这样的情节:

enter image description here

0

另一种方式是将楼梯转换为多边形并使用polybool/polyxpoly设置操作器

function patch = sorted2patch(st) 
    patch=kron(st,[1 1]); 
    patch(2,3:2:size(patch,2)-1)=patch(2,2:2:size(patch,2)-1);  
    if skewness(st(1,:)) > 0 
     patch(2,1)=patch(2,end); 
    else 
     patch(1,1)=patch(1,end); 
    end  
end 

pc_bh = [0.99754 0.99754 0.99772 0.99790 0.99808 0.99821 0.99842 0.99870 0.99886 0.99900 0.99918; ... 
1  0.9  0.8  0.7  0.6  0.5  0.4  0.3  0.2  0.1  0; ... 
0.99873 0.99873 0.99899 0.99918 0.99928 0.99945 0.99969 0.99973 0.99987 0.99993 0.99993; ... 
0  0.11111 0.22222 0.33333 0.44444 0.55555 0.66666 0.77777 0.88888 1  1]; 
patch1=sorted2patch(pc_bh(1:2,:)); 
patch2=sorted2patch(pc_bh(3:4,:)); 
[xand,yand]=polybool('and',patch1(1,:),patch1(2,:),patch2(1,:),patch2(2,:)); 
figure, stairs(pc_bh(1,:),pc_bh(2,:),'r'), 
hold on, 
stairs(pc_bh(3,:),pc_bh(4,:),'b') 
patch(xand,yand,'y') 
相关问题