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

How can I highlight the overlapping area in a stairstep graph?

我正在尝试突出显示两个阶梯图的交叉区域。我能够 select 相交区域内的点,并想用 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))

并与它们相交,这也没有用。

这是想要的结果:

这是一个在交叉区域内的点上带有标记的图:

标记的代码非常简单:

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

困难在于每个阶梯图都是在一组不同的 x 值下进行评估的。本质上,您需要将每个阶梯图的值插入到另一个阶梯图的相应 x 值处,以便您可以比较相同点的 y 值以查看哪个是最小值。 Normal interpolation runs into problems because you have to have repeat x values in a stairstep plot. An alternative is to use the histcounts function to find for each plot which steps their points fall on for the other plot. Here's a function stairarea that illustrates this, taking two sets of x and y data as input and creating a plot using stairs and area:

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

你会得到这个情节:

另一种方法是将楼梯转换为多边形并使用 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')