Gaussian Contour plot of 3 variables - MATLAB

1k views Asked by At

I have generated a 3D plot that resembles Gaussian distribution, with random variables Y, X1, and X2 (1000x1) vectors. Y is on the vertical axis, X1, and X2 are horizontal.

Specifically, this is the code I used for the plot:

plot3(x(:,1),x(:,2),y,'.')

The graph that has been created has this form:

3D - Gaussian

What I also want to produce is something like that:

Contour - Gaussian

But, when I use this code:

contour(x(:,1),x(:,2),y);

I receive a message that:

Error using contour (line 48) Z must be at least a 2x2 matrix.

I really don't get how to fix that problem, I assume Z is the Y but I don't understand why it has to be 2x2 at least. Anyhow, any help would be much appreciated.

1

There are 1 answers

0
Iban Cereijo On

You cannot create a contour over scattered data, you need a grid. It is possible to interpolate the data on a grid of NxN samples in the XY domain, using griddata (here the domain is [-2,2]x[-2,2] as an example):

N = 200;
xi = linspace(-2, 2, N);
yi = linspace(-2, 2, N);
[XI, YI] = meshgrid(xi, yi);
ZI = griddata(x(:,1), x(:,2), y, XI, YI, 'v4');
contour(XI,YI,ZI);

More info on how to interpolate scattered data here.