如何"crop" MATLAB plot 变成三角形?

How to "crop" the MATLAB plot into a triangular one?

我有一个二维散点图,其中所有的点实际上都保证落入等边三角形。所以我希望 "crop" 感兴趣的区域,即三角形,超出默认的正方形 x-y 系统。

我该怎么做?如果提供的方法可以轻松扩展到正方形、等边五边形等就更好了。

P.S.:等边三角形的顶点为[[0 0.577350269189626];[0.500000000000000 -0.288675134594813];[-0.500000000000000 -0.288675134594813]].

使用inpolygon to detect which points are inside the polygon. This works for any polygon, not just triangles. You can then remove the axes and plot the polygon manually with patch产生你想要的效果。

%// Define data:
x = rand(1,1000)-.5;
y = rand(1,1000)*.9-.3;
p = [0 0.577350269189626;
     0.500000000000000 -0.288675134594813;
     -0.500000000000000 -0.288675134594813];

%// Plot data before cropping
figure
plot(x,y,'o');

%// Select points inside the polygon:
ind = inpolygon(x,y,p(:,1),p(:,2));

%// Plot data after cropping
figure
plot(x(ind),y(ind),'o');

%// Plot cropped data with polygon and without axes:
figure
patch(p(:,1), p(:,2), 'w', 'edgecolor', 'r') %// polygon white background and red border
hold on
plot(x(ind),y(ind),'o'); %// plot points after polygon
axis off %// remove axes

以下是三个示例图: