Creating 2D contours from XYZ values

I need to create a 2D contour map from the following X,Y,Z values:
x =
62158.9992780663
-60909.305073411
-66390.9511725443
39368.6900415121
51352.5822375015
-21369.5783460914
-87076.7531674554
49625.0647256742
38699.5563685481
52310.578005926
-66729.7609269372
y =
-464698.41308258
316839.903892009
298087.560112334
-499661.758015527
298806.606131584
129251.742701853
256843.382855648
-367726.4174525
-566816.147075169
320336.240040152
278737.299892196
z =
266.712
37.838
7.79
301.42
145.949
145.305
112.418
289.788
268.437
-10.679
201.505
The X and Y values are grid coordinates. The Z values are ellipsoidal heights in metres.
I tried using the following code:
contour(x,y,z)
And I get this error:
Error using contour (line 48)
Z must be at least a 2x2 matrix.
Error in Contours (line 284)
contour(x,y,z)
Can anyone help me create a contour map with contour lines every 10m?

 Accepted Answer

This is likely the best you can do:
xv = linspace(min(x), max(x), numel(x));
yv = linspace(min(y), max(y), numel(y));
[Xm,Ym] = ndgrid(xv, yv);
Zm = griddata(x, y, z, Xm, Ym);
figure
contourf(Xm, Ym, Zm)
grid
This creates monotonically-increading vectors from the original ‘x’ and ‘y’ vectors using linspace, creates corresponding matrices using ndgrid, then interpolates them using griddata to create the matrices necessary for the contour function. (The grid call is optional.)

5 Comments

This helped a lot thanks. How come the contours only go down to 140m? When looking at my z matrix above, some points are as low as -10m in elevation.
The image below is what I get after making er look real nice with titles and a colorbar. I changed my mind and went with Lat and Long instead of Grid coordinates.
Nevermind I figured that out. I have another question if you don't mind. I will be making several of these maps, but with z matrices that slightly differ. How do I make it so the range of vertical axes for all the graphs are the same so that comparing the graphs will be easy?
The ylim (or xlim or axis) functions will allow you to specify the axis limits.
I did not realise this had something to do with topography. (I assume those will work with the topographic axis labels, however I cannot test that.)
I am wondering if there is any way to ensure that the contour map goes to the extents of the project area? I'm needing the contour map to continue out to the + marks I've plotted. The plus marks are X and Y coordinates above.
If I remember correctly, the blank areas are because griddata is extrapolating, and except for the 'v4' method, extrapolated values are NaN and so don’t plot. Experiment with 'v4' to see if it does what you want. See the documentation section on method for details.
If there is a Mapping Toolbox function that approximates this, it may be preferable to griddata. I don’t have the Mapping Toolbox, so I can’t determine that.

Sign in to comment.

More Answers (1)

KSSV
KSSV on 18 May 2020

1 Comment

I found this link very confusing. I like the way tricontour looks but I can't figure it out.

Sign in to comment.

Categories

Tags

Community Treasure Hunt

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

Start Hunting!