【问题标题】:How can I highlight the overlapping area in a stairstep graph?如何突出显示阶梯图中的重叠区域?
【发布时间】:2018-02-11 12:42:24
【问题描述】:

我试图突出显示两个阶梯图的相交区域。我能够选择相交区域内的点,并想使用 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

【问题讨论】:

    标签: matlab plot matlab-figure


    【解决方案1】:

    困难在于每个阶梯图都是在一组不同的 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, :));
    

    你会得到这个情节:

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 2012-08-21
        • 2018-07-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多