How do I plot a contour map of non-uniform x,y,z data?

I have data points that each have an x position, a y position, and a height. The points are not uniformly spaced or in a specific order. Scatter3 plots the data fine, but I need to make a contour plot of the heights. Contour and contour3 seem to require a full, evenly spaced n x m array of points for x, y, and z, but I don't have that. From what I read, meshgrid should be used to interpolate the data, but I can't get it to work. I can create a grid for x and y, but the z (height) values don't line up with any of the new grid points. Can someone describe how to do this?

 Respuesta aceptada

Chad Greene
Chad Greene el 25 de Abr. de 2016
Hi James,
This is a common problem with scattered data. What you have are some real measurements (plus noise) at a few locations, and the first step you need to do is to create some data everywhere you don't actually have data. I want to stress that all gridded or contoured data are effectively made up, based on a few real measurements (plus noise). But you can be thoughtful about how to fill in everywhere you don't actually have data.
Matlab has a function called griddata and another called scatteredInterpolant, but I've never been very impressed with them. John D'Errico's gridfit, on the other hand, is computationally efficient and quite easy to use. You'll have to tune the smoothing parameter to match your data the way you'd like it to.
After you've gridded your data, contour will be easy to use.

6 comentarios

John D'Errico's gridfit worked great! Being relatively inexperienced, it took me a while to set up the xnodes and ynodes for the gridfit function, and to set up the xgrid and ygrid for the x,y position of each point of the zgrid created by the gridfit function. Here's what I did:
x=pts(:,1); %get x values from pts(x,y,z)
y=pts(:,2); %get y values from pts(x,y,z)
z=pts(:,3); %get z values from pts(x,y,z)
xmin=min(x); %find minimum x value
xmax=max(x); %find maximum x value
xnum=fix((xmax-xmin+1)/5); %set number of x points to use for grid
ymin=min(y); %find minimum y value
ymax=max(y); %find maximum y value
ynum=fix((ymax-ymin+1)/5); %set number of y points to use for grid
xnodes=linspace(xmin,xmax,xnum); %set nodes for the x direction
ynodes=linspace(ymin,ymax,ynum); %set nodes for the y direction
[xgrid,ygrid]=meshgrid(xnodes,ynodes); %create the x and y grids
zgrid=gridfit(x,y,z,xnodes,ynodes); %create the z grid (z for each x,y)
zgridmin=284; %set the minimum elevation desired
zgridmax=318; %set the maximum elevation desired
[zgridx,zgridy]=size(zgrid); %get the size of the grid
%go through the grid and adjust any points out of desired range
for i=1:zgridx
for j=1:zgridy
if zgrid(i,j)<zgridmin
zgrid(i,j)=zgridmin;
end
if zgrid(i,j)>zgridmax
zgrid(i,j)=zgridmax;
end
end
end
%create plot with lines at integer values by picking proper number to plot
[C,h]=contour(xgrid,ygrid,zgrid,(zgridmax-zgridmin-1));
set(h,'ShowText','on','TextStep',get(h,'LevelStep'))
Hi. I tried this code in Matlab R2017b, however, errors come up and say 'gridfit'is an undefined function!
Chad Greene
Chad Greene el 10 de Sept. de 2019
That's right. gridfit is on the File Exchange and a link is provided in the answer above.
I am going to give gridfit() a try. Will let you know my results
Hossein Sahhaf
Hossein Sahhaf el 11 de Oct. de 2020
Editada: Hossein Sahhaf el 11 de Oct. de 2020
Hi Chad
Thanks for your good offers. I had a problem like that James was encountered with. I used the griddata function after reading your answer and my problem was solved.
Thanks a lot!
Hi, Please correct me if I am wrong, does not 'meshgrid' generate uniform mesh? How did 'gridfit' solved the issue of non-uniformely gridded data?

Iniciar sesión para comentar.

Más respuestas (2)

Mingta Yang
Mingta Yang el 5 de Mzo. de 2021
If there is connectivity info for the scattered data, fill3 might be what you need. It plots filled polygons in 3D.
Shivam Anand
Shivam Anand el 11 de Mayo de 2022
Editada: Shivam Anand el 11 de Mayo de 2022
x=[32 20 67 1 98 34 57 65 24 82 47 55 8 51 13 14 18 30 37 39 10 33 21 26 38 81 83 60 95 22 17 5 72 46 99 52 12 25 96 29 70 85 43 69 19 78 97 31 89 53 2 91 48 71 61 15 36 84 94 50 11 80 6 7 49 74 9 88 40 79 27 68 73 64 63 59 86 23 35 58 45 28 100 42 93 87 16 90 41 66 54 92 77 4 62 76 75 56 3 44];
y=[96 75 24 9 83 49 27 77 3 23 17 31 40 13 7 52 51 21 98 47 64 79 78 91 44 16 15 100 84 99 63 68 70 30 54 76 97 73 33 5 88 8 71 66 62 25 60 42 72 45 18 11 28 59 89 65 10 55 69 81 12 26 20 95 87 41 74 50 93 22 43 90 14 34 82 35 56 38 80 32 1 57 6 36 37 61 29 58 2 48 4 46 67 53 92 86 94 19 39 85];
z=[55 31 11 45 83 36 86 49 15 57 42 46 8 94 88 47 54 81 98 41 32 35 56 85 9 89 37 60 23 62 67 100 78 76 73 80 10 20 68 34 77 93 1 63 53 12 22 99 91 40 84 24 33 3 43 19 92 97 6 82 64 25 26 79 95 4 44 58 5 21 70 29 65 87 96 90 51 14 18 2 72 28 71 39 52 7 27 59 50 61 48 30 66 69 17 13 74 16 75 38];
xlin = linspace(min(x), max(x), 100);
ylin = linspace(min(y), max(y), 100);
[X,Y] = meshgrid(xlin, ylin);
% Z = griddata(x,y,z,X,Y,'natural');
% Z = griddata(x,y,z,X,Y,'cubic');
Z = griddata(x,y,z,X,Y,'v4');
mesh(X,Y,Z)
axis tight; hold on
plot3(x,y,z,'.','MarkerSize',15)

Categorías

Más información sobre 2-D and 3-D Plots en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 25 de Abr. de 2016

Editada:

el 11 de Mayo de 2022

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by