填充使用 patch 函数绘制的矩形

Filling in rectangles drawn with the patch function

我想在我正在创建的图形上绘制矩形。我还希望能够用透明颜色填充该矩形。所以我使用 'Patch' 而不是 'Rectangle' 这将允许我这样做。

我能够得到我想要的位置和大小的矩形,但我似乎无法按照我 认为 应该的方式填充透明度工作?

在文档中,它的形式是:

p=patch(X,Y, 'c')

当我输入这个时,它从不考虑 'c'/cyan 表示的颜色,只会绘制一个标准的黑色矩形。但是,我可以使用点索引符号设置颜色:

p.EdgeColor='c';

我的实际脚本不同,因为 X 和 Y 是可变的,但我在这里附加了一个非常简单的脚本来模拟我的矢量化绘图问题。它只是在每 10 个位置创建 Xs 个顶点,宽度为 2,高度为 10。

我是不是误用了补丁函数让它用 FaceAlpha 填充透明颜色?

clear all;

% Generate some simple X value data, where each X is the X value starting from the
% lower left of the rectangle, in a clockwise fashion around the edges of
% rectangle.
X1(:,1) = 0:10:100;
X2(:,1) = 0:10:100;
X3(:,1) = X1+2;
X4(:,1) = X1+2;

% Rotate the X vectors into rows. 
X1 = X1(:)';   
X2 = X2(:)';
X3 = X3(:)';    
X4 = X4(:)';

% Here I am stacking each X on top of each other, and padding the bottom
% row with NaNs.  Adding a similar Y set of values, which will simply be a
% height of 0-10
X =[X1; X2; X3; X4; 
    nan(size(X1))   ];
Y =[zeros(size(X1));        10+zeros(size(X2)); ...
    10+zeros(size(X3));     zeros(size(X4));     
    nan(size(X1))  ];


% Rotate vectors so that the last value in the X/Y coordinates is a NaN,
% and Matlab will ignore and plot the next value
X=X(:);
Y=Y(:);

% This Plots the outline of the rectangles I am looking for, with a width
% of 2, height of 10, starting at each 10th value of X
p=patch( X ,Y,'c');
p.EdgeColor = 'b';  %Comment this out to see that the Patch function is not working?
p.FaceColor = 'blue';
p.FaceAlpha = 0.5;

% But for some reason it is not FILLING in the rectangles/patches I have
% drawn?

如果您想创建多个矩形,您不想用 NaN 值来分隔它们,而是想将每个矩形的坐标放在单独的列中。

X = [X1; X2; X3; X4];

Y =[zeros(size(X1));        10+zeros(size(X2)); ...
    10+zeros(size(X3));     zeros(size(X4))];  

patch(X, Y, 'c')

此外,patch 的第三个输入指定了面的颜色 (FaceColor),但默认 EdgeColor 将是黑色。

您可以将 parameter/value 对指定为 patch 的附加输入,以指定任何其他参数(例如 EdgeColor)的所需值。

patch(X, Y, 'c', 'EdgeColor', 'c', 'FaceAlpha', 0.5)

您的问题不在于颜色,而在于 XY,它们应该是 4-by-n matrix。这是一种方法:

x = [0:10:100; (0:10:100)+2];
X = reshape(repelem(x(:),2),4,[]);
Y = repmat([0; 10; 10; 0],1,size(X,2));
p = patch(X,Y,'b','EdgeColor','c', 'FaceAlpha', 0.5);