如何突出阶梯图中的重叠区域?

时间:2017-09-02 19:20:40

标签: matlab plot matlab-figure

我试图突出两个阶梯图的交叉区域。我能够选择交叉区域内的点,并希望使用patch命令创建填充的形状,但是没有成功。但是,仍然必须排除某些点,并且必须添加交叉点。

另一个想法是创建两个面积图,看起来像阶梯图:

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值,第3 /第4行包含第二个图形的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 个答案:

答案 0 :(得分:2)

难度来自于每个阶梯图在不同的x值集合下评估的事实。基本上,您需要在另一个的相应x值处插入每个阶梯图的值,以便您可以比较相同点处的y值以查看哪个是最小值。 Normal interpolation会遇到问题,因为你必须在阶梯状图中重复x值。另一种方法是使用histcounts函数为每个绘图找到他们的点落在另一个绘图上的步骤。这是一个函数stairarea,用于说明这一点,将两组x和y数据作为输入,并使用stairsarea创建一个图:

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

答案 1 :(得分:0)

另一种方法是将楼梯转换为多边形并使用polybool / polyxpoly

使用set operatons
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')