# Advanced MATLAB: Surface plot of nonuniform data 23

Posted by **Doug Hull**,

Two minute video shows how to fit a surface to nonuniform data.

Real data is not always on a nice X,Y grid, but the MATLAB commands

SURF and MESH expect them to be. GRIDDATA and MESHGRID help to fix this.

Other videos have been gathered here:

http://blogs.mathworks.com/pick/category/video/

**Category:**- Advanced MATLAB,
- Video

## 23 CommentsOldest to Newest

**1**of 23

Dear Sir:

I am very grad to find the program of “advanced-matlab-surface-plot-of-nonuniform-data”, but I could not run it

because of no-coincide of matrix dimension.

Would you please inform me of a mistake I coded (copied).

Best Regards,

————————

%3D graph for nonuniform_data

x=rand(100,1)*16-8;

y=rand(100,1)*16-8;

r=sqrt(x.^2+y.^2)+eps;

z=sin(r)./r;

%

xlin=linspace(min(x),max(x),33);

ylin=linspace(min(y),max(y),33);

[X,Y]=meshgrid(xlin,ylin);

Z=griddata(x,y,z,X,Y,’cubic’);

%

mesh(X,Y,Z); % interpolated

axis tight; hold on

plot3(x,y,z,’.’,’MarkerSize’,15)

%%surf(X,Y,Z)

**2**of 23

I copied the code from you message and it worked perfectly on my side! – sorry!

**3**of 23

The code you pasted here worked exactly right for me too. I am not sure what to do for you.

Sorry,

Doug

**4**of 23

Greg pointed me to this solution he wrote a while back that might be of interest also:

Tech Note 1212 – Using MESHGRID and GRIDDATA to Fit Vector Data and Plot Unevenly Spaced Data http://www.mathworks.com/support/tech-notes/1200/1212.html

**5**of 23

Dear Doug,

This info about making a surface plot was very usefull for me. In stead of the formule that you used to produce some points, I used 4 XYZ-coordinates that result in an surface in 3D. Is there an easy way to calculate the surface area of this created object?

Best regards,

Els

**6**of 23

Els,

There is no built in function to do this, but knowing the X,Y,Z of each point in the mesh, you should be able to calculate the area of each four point patch. From there, summing should be easy to know the whole area.

Doug

**7**of 23

I have three column data (time,height,collection) supposedly x,y,z data. I want to plot it and get the equation. How can I do that with Matlab.

eg data:

x – y – z

1 – 1 – 130

2 – 1 – 130

3 – 1 – 130

4 – 1 – 130

7 – 2 – 130

10 – 2 – 130

16 – 2 – 130

25 – 2 – 130

40 – 2 – 130

63 – 3 – 130

100 – 3 – 130

160 – 3 – 130

250 – 3 – 130

300 – 3 – 130

400 – 3 – 130

500 – 3 – 130

1 – 3 – 260

2 – 3 – 260

3 – 3 – 260

4 – 3 – 260

7 – 3 – 260

10 – 3 – 260

16 – 4 – 260

25 – 4 – 260

40 – 4 – 260

63 – 6 – 260

100 – 8 – 260

160 – 10 – 260

250 – 11 – 260

300 – 11 – 260

400 – 11 – 260

500 – 11 – 260

1 – 4 – 390

2 – 5 – 390

3 – 5 – 390

4 – 5 – 390

7 – 6 – 390

10 – 7 – 390

16 – 8 – 390

25 – 10 – 390

40 – 12 – 390

63 – 14 – 390

100 – 15 – 390

160 – 19 – 390

250 – 22 – 390

300 – 26 – 390

400 – 30 – 390

500 – 30 – 390

1 – 4 – 650

2 – 5 – 650

3 – 5 – 650

4 – 5 – 650

7 – 5 – 650

10 – 7 – 650

16 – 8 – 650

25 – 9 – 650

40 – 12 – 650

63 – 14 – 650

100 – 18 – 650

160 – 22 – 650

250 – 27 – 650

300 – 27 – 650

400 – 36 – 650

500 – 40 – 650

1 – 2 – 910

2 – 2 – 910

3 – 2 – 910

4 – 3 – 910

7 – 5 – 910

10 – 5 – 910

16 – 7 – 910

25 – 9 – 910

40 – 15 – 910

63 – 20 – 910

100 – 24 – 910

160 – 32 – 910

250 – 38 – 910

300 – 41 – 910

400 – 46 – 910

500 – 49 – 910

**8**of 23

I have multiple [x,y,z] matrices of my data with which i want to plot the surface using a 7th order polynomial, how can I do that??

**9**of 23

Venkat,

I am not sure that I understand the question. If you have x,y,z triples, you just plot them. Are you trying to fit a polynomial to them? That would imply a two dimensional dataset. Are you trying to fit some kind of a surface to the data?

You might be best off looking here:

http://blogs.mathworks.com/pick/how-to-get-help/

It shows how to ask for help at the bottom.

Doug

**10**of 23

Hi Doug

Thanks for these videos, they have really helped me alot. I am currently looking to create a surface plot (non uniform data) but I would like to represent it in the z-axis. To explain myself better:

If you think of various layers of semi circles made up of dots, and as you move ‘up’, the number of dots are increasing, so it ends up looking like a funnel. Now the code you presented here creates a surface for each circle (ie. for each layer), but now I want to create a surface joining the layers. I hope I have explained myself adequately. Thanks again and I would really appreciate your input.

Kind Regards

**11**of 23

@Daniel,

I think you would be well served to look at the series about volume visualization. It starts here:

The one about isosurfaces will be posted today or tomorrow (11/10/2009). That is very likely the function you seek.

Doug

**12**of 23

Dear Doug

Is there anyway to plot a surface from nonuniform data without meshgrid and griddata? Basically i have topographic information, where the altitude(z) at each x and y is known.

I do not want any interpolation in between. x and y do not form a rectangular grid but are closely spaced.

Thanks

**13**of 23

@Teja,

I think this will work:

http://www.mathworks.com/access/helpdesk/help/techdoc/ref/delaunaytriclass.html

Doug

**14**of 23

i like to use this program..but can someone explai to me, what the number meant ,100,1,16,8 and 33 for below..

x=rand(100,1)*16-8;

y=rand(100,1)*16-8;

xlin=linspace(min(x),max(x),33);

i try to run randomly, but appear error with “cubic” like

The input character is not valid in MATLAB statements or expressions.

Thanks

**15**of 23

@Faiz

X is a vector of random numbers 100×1. It is then multiplied by 16 and 8 is subtracted from it.

xlin is a vector of lineraly spaced numbers (33 in total) that range from the minimum to the maximum of x.

**16**of 23

hello

i have 3 sets o data x,y,z and they are nonuniform

how can i make a contour plot of z values?

**17**of 23

they are bathymetric data which x and y are coordinations and z is the depth at that point

i need contour lines of z value

**18**of 23

Hi;

I have written the following script based on your lesson. The surface which i end up in seems not be correctly reflect the data point i have. Would you please have a look and tell me what could be wrong here?

X1=[7 8 10 12 8 10 12 12 12 12 12];

Y1=[15.8 16.8 17.8 18.8 17.8 17.8 17.8 15.8 16.8 17.8 18.8];

Z1=[12.6 16.2 36 57.9 47.4 36 30.1 16.4 25.7 30.1 57.9];

CLF();

Xlin=linspace(min(X1),max(X1),30);

Ylin=linspace(min(Y1),max(Y1),30);

[X,Y]=meshgrid(Xlin,Ylin);

Z=griddata(X1,Y1,Z1,X,Y,’Cubic’);

mesh(X,Y,Z);

%axis tight, hold off

%plot3(X1,Y1,Z1,’.’,’Markersize’,15)

%surf(X,Y,Z);

**19**of 23

Thanks for the fantastic demonstration. I tried to figure out how to generate surface plots for non-uniform data a couple of times before but never found somebody who was able to explain it to me. I got it know and my surface plot looks fantastic. At those moments I love the internet and the helpful guys like you who give their time to help me understand…

Thank you so much.

**20**of 23

Hi,

I have a set of x,y,z data. The data is mainly SNR,RSRP and Throughput in x,y,z dimensions respectively. While i perform the steps you’ve mentioned, i get the following error:

??? Error using ==> DelaunayTri

The input points must contain finite values; Inf and NaN are not permitted.

Error in ==> griddata>cubic at 200

dt = DelaunayTri([x(:) y(:)]);

Error in ==> griddata at 136

zi = cubic(x,y,z,xi,yi);

Can you help me with it?

**21**of 23

Hello,

The code works well, but for relatively small arrays.

I have over 300,000 points, so griddata doesn’t have enough memory to handle the data.

TriScatteredInterp doesn’t work well for me too, because the surface it generates isn’t smooth.

Any suggestions for me?

**22**of 23

I also have three data colunn, X, Y, Z as:

X Y Z

0.1 0 90

3.5 0 45

4.9 0 65

6.2 0 40

7 0 55

9 0 25

9 5 55

9 3 48

9 7 45

6.5 7 75

4.5 7 50

2.9 7 75

1.3 7 52

0 7 70

0 4.1 90

0 2.1 105

1.7 5.6 75

2.2 4.5 66

2.5 3.6 60

2.9 2.4 55

3.2 1.1 50

1.6 6.6 60

4.7 1 66

4.6 1.6 70

4.5 2.5 80

4.6 3.6 95

4.5 4.2 80

4.3 5.1 70

4.4 6 60

5.3 5.3 78

6 5.7 88

6.9 5.6 102

7.1 5 104

7 3.5 90

6.9 2.7 80

6.9 1.9 70

7 0.6 60

6 1 51

6 2 54

5.9 3 60

6 4 64

6.3 4.8 71

3 6 75

4 4.5 75

5 4.5 73

0.6 5 80

1.8 2 70

Anyone can assist me to plot mesh or surface plot in 3D. Thanks

**23**of 23

I am also trying to make a 3D plot similar to the the one above. How would I do this if I dont have a function for z.

## Recent Comments