shamalaia
shamalaia

Reputation: 2347

understanding patch behavior

I do not understand why patch does not follow the lines in this image:

ex

In fact, in the code I used, the lines and the patch share the same input variables X and Y:

clearvars
close all
clc

figure(1)
z=peaks(50);

% Create x,y coordinates of the data
[x,y]=meshgrid(1:50);

% Plot Data and the slicing curve
surf(z);
hold on

X=[1 21 35 47 29 25 8];
Y=[5 19 24 26 14 39 47];

plot3(X,Y,-10*ones(1,numel(X)),'r','linewidth',3);
plot3(X,Y,10*ones(1,numel(X)),'r','linewidth',3);

patch([X fliplr(X)],[Y fliplr(Y)],[-10*ones(1,numel(X)) 10*ones(1,numel(X))],...
    'r','FaceAlpha',0.21)

axis([0 50 0 50])

What am I messing up?

Upvotes: 2

Views: 33

Answers (1)

AVK
AVK

Reputation: 2149

You pass the wrong arguments to patch. The Matlab documentation says that

If XData is a matrix, then each column represents the x-coordinates of a single face of the patch.

Thus, the patch data should look like this:

XData= [
X(1) X(2) ... and so on
X(2) X(3)
X(2) X(3)
X(1) X(2)];

YData= [
Y(1) Y(2) ... 
Y(2) Y(3)
Y(2) Y(3)
Y(1) Y(2)];

ZData= [
-10 -10 ...
-10 -10
 10 10
 10 10];

You can use patch this way:

patch([X(1:end-1);X(2:end);X(2:end);X(1:end-1)],[Y(1:end-1);Y(2:end);Y(2:end);Y(1:end-1)],...
    [-10*ones(1,numel(X)-1);-10*ones(1,numel(X)-1);10*ones(1,numel(X)-1);10*ones(1,numel(X)-1)],...
    'r','FaceAlpha',0.21)

or you can use surf:

surf([X;X],[Y;Y],[-10*ones(1,numel(X)) ;10*ones(1,numel(X))],'FaceAlpha',0.21);

Upvotes: 2

Related Questions