Search code examples
matlabplotmatlab-figurepatchrectangles

Filling in rectangles drawn with the patch function


I am wanting to draw rectangles on a figure that I am creating. I also would like to be able to fill that rectangle with a transparent color. So I am using 'Patch' instead of 'Rectangle' which will allow me to do so.

I am able to get the rectangles in the location and the size that I want, but I cannot seem to get the filling in with transparency the way that I think it should work?

In the documentation, it is of the form:

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

When I enter this in, it never takes into account that Color denoted by 'c'/cyan, and will just plot a standard black rectangle. However, I can set the color with the dot index notation of:

p.EdgeColor='c';

My actual script is different as the X's and Y's are variable, but I am attaching a very simple script here to simulate my issue of my vectorized plotting issue. It simply creates Xs vertices at every 10th location, with a width of 2, and a height of 10.

Am I misusing the patch function to get it to fill with a transparent color with 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?

Solution

  • If you want to create multiple rectangles, you don't want to put the NaN values to separate them, you instead want to put each rectangle's coordinates in a separate column.

    X = [X1; X2; X3; X4];
    
    Y =[zeros(size(X1));        10+zeros(size(X2)); ...
        10+zeros(size(X3));     zeros(size(X4))];  
    
    patch(X, Y, 'c')
    

    Also, the third input to patch specifies the color of the faces (FaceColor) but the default EdgeColor is going to be black.

    You can specify the parameter/value pairs as additional inputs to patch to specify the desired value for any other parameters (such as EdgeColor).

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

    enter image description here