2017-09-02 4 views
2

私は2つの階段状グラフの交差する領域を強調表示しようとしています。私は交差領域内の点を選択することができ、うまくいかなかった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)) 

し、それらを交差し、:

もう一つのアイデアは、と階段のグラフのように見える2つの面積グラフを作成することでした。ここ

は、所望の結果は次のとおり

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'); 

データセットは、第1 /第2行が第1グラフのx/y値を含み、第3 /第4行が第2グラフのx/y値を含む4×11行列である。

これは、使用されるデータセットである:

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値を比較してどちらが最小かを見ることができます。階段状プロットで繰り返しx値を指定する必要があるため、Normal interpolationに問題が発生します。もう1つの方法は、histcounts関数を使用して、各プロットに対して、そのプロットのポイントが他のプロットに対してどのステップにあるかを見つけることです。ここではこのことを示して機能stairareaは、入力として、xとyの2つのデータセットを取り、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

別の方法があります階段をポリゴンに変換し、ポリブール/ポリックスポリを使用して集合演算を使用する

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') 
関連する問題