填充使用补丁功能绘制的矩形

时间:2016-10-27 21:36:28

标签: matlab plot matlab-figure patch rectangles

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

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

在文档中,它的格式为:

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

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

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?

2 个答案:

答案 0 :(得分:2)

如果您想创建多个矩形,您不希望将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将为黑色。

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

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

enter image description here

答案 1 :(得分:1)

您的问题不是颜色问题,而是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);

patch

相关问题