1
votes

I need to be able to do a surface plot using data from 3 vectors. I found similar information, but no method seems to work with my data. My X and Y columns are evenly spaced, but not in increasing order. I tried different methods, but none of them seem to give me what I want, which is a simple surface linking close points together. I tried the following:

[X Y]=meshgrid(x,y);
Z=griddata(x,y,z, X,Y);
surf(X,Y,Z);

This is not exactly what I want, because it creates a surface at z=0 and makes it look more like a volume plot than just a surface. It also runs very slowly on my computer (probably from creating all the gridpoints). If I could get something that doesn't require as much memory it would be ideal (my vectors have about 20k values each), but this is not a necessity.

***Edit: I also tried using the scatteredInterpolant method found here,but the function doesn't seem to be recognized by MATLAB and I get this error:

Undefined function 'scatteredInterpolant' for input arguments of type 'double'.

Also here is an image of my problem: here

You can see that we can't see under the surface, there is some z=0 plane blocking it.

If you have anything for me, any help is appreciated.

Thanks in advance.

**Edit 2: I added sample vectors, they're my x,y and z values from left to right.

***Edit 3: Here's an image of the triangulation I get. As you can see some points are being ignored for some reason, which gives those long and weird looking blue triangles.

Mike

3
Does it help if you do Z(~Z)=NaN? Or what if you use a different method as described in doc griddata? Otherwise please post some sample code and vectors to fully replicate the problem. - Dennis Jaheruddin
@DennisJaheruddin When I do Z(~Z)=NaN I get a flatter bottom but I still can't see through it, it closes it and forms a volume. My vectors are quite big, I'll try to find a way to link them. - Mike Nelson
Are you sure your data doesn't actually contain the low values that are plotted? What is the minimum z where the x and y value are between 0.8 and 1.2? Also, don't forget to try the different griddata options. - Dennis Jaheruddin
@DennisJaheruddin Yes, when doing a scatter plot, we can clearly see that there are no points under the surface. I tried all methods in griddata and they all look similar. Is there any way to do it without using a grid? Is there no options that just looks at scattered points and tries to link them with a surface if they're close enough from one another? The grid will make the edges squares and it's not really desireable/ The delaunay method almost achieves that but there seems to be an error with the triangulation, which gives weird links, colors, and the same z=0 plane problem. - Mike Nelson

3 Answers

1
votes

As conventional methods seem to fail, I would suggest you to do it manually.

  1. Create a Z matrix full of NaN values. The size of the matrix should be dependant on your x and y values.
  2. Loop over all occuring x,y, pairs and put their (average?) z value in the right position of your Z matrix.
  3. Loop over all NaN values and interpolate their value. Perhaps using filter2.
  4. Use surf to plot the resulting surface
1
votes

If you have points which are described by vectors, and you want to plot them you could always use a Delauny triangulation. The function in matlab is called Tri=delauny(X,Y,Z). The data generated by this function can be shown with either trimesh(Tri,X,Y,Z) or trisurf(Tri,X,Y,Z). Keep in mind trisurf is only for 3D data. If you want to adjust the transparancy of plots in your graph use the alpha setting.

I hope this helps

1
votes

To me it looks like you just need to sort your data before plotting.

Here is an example which I believe is similar to your case (since I could not download your data).

x = [2 1 4 3 -1 -3 -4 -2];
y = [1 2 3 4 -1 -2 -3 -4];
z = 32 - x.*x - y.*y;

[X1 Y1] = meshgrid(x,y);
Z1 = 32 - X1.*X1 -Y1.*Y1; 
surf(X1,Y1,Z1)

aux = sort([x;y],2);
x = aux(1,:);
y = aux(2,:);

[X2 Y2] = meshgrid(x,y);

Z2 = 32 - X.*X - Y.*Y;
figure()
surf(X2,Y2,Z2)

The first figure results in a very problematic surface:figure 1 (unsorted plot)

The second figure contains the desired surface:figure 2 (sorted plot)