MATLAB Answers

How do I plot interpolated grid data and original datapoint set in the same plot?

7 views (last 30 days)
NP on 16 Sep 2019
Commented: NP on 18 Sep 2019
After creating a grid data series (Data Terrain Model) from interpolating contour lines, I would like to plot everything on the same layer, but I am not sure if it is possible in matlab. Here is the gist of what I've done:
[xi,yi] = meshgrid(Xmin:res:Xmax, Ymin:res:Ymax);
f = scatteredInterpolant(contour_grid.X,contour_grid.Y,contour_grid.Z);
zi= f(xi,yi)
Xi=[xi(:); contour_grid.X];
Yi=[yi(:); contour_grid.Y];
Zi=[zi(:); contour_grid.Z];
Is there a way to plot Xi, Yi, and Zi? I'd have to turn these arrays back into grids to be able to use mesh(X,Y,Z) but I am struggling to write how to do that.
Has anyone ever had to do that?


Sign in to comment.

Accepted Answer

Bjorn Gustavsson
Bjorn Gustavsson on 17 Sep 2019
You can always simply plot all your points:
Or with scatter:
But to me this seems to be more interesting:
% shading flat % or interp adjust shading to fit taste
hold on
Or you could compare contour-plots:
Levels = linspace(min(Zi(:)),max(Zi),10);
hold on
You can find the tricontour function on the file exchange: TRICONTOUR


NP on 17 Sep 2019
Thanks for this elaborate answer. I had a go at the different functions and they give interesting results. Ploting all the points is a bit taxing for the computer though. Ideally, I'd like to plot Xi, Yi and Zi that include the interpolated points and the original contour line points in a mesh, but can a mesh function even do that? Not sure.
Bjorn Gustavsson
Bjorn Gustavsson on 18 Sep 2019
This whish of yours confuse me. First you say that "plotting all the points" are too taxing, then you say that you want to plot Xi, Yi and Zi - that are explicitly all points you have.
The terse answer is: matlab's mesh function cannot do what you want. It requires, just as its plot-function siblings pcolor, surf et al., points in 2-D arrays (you have such for xi, yi and zi, but your contour_grid.X, contour_grid.Y, and contour_grid.Z are in 1-D arrays. It is not straightfor (impossible) ward to merge these into 1 set of regular 2-D arrays).
You might get what you request with the trimesh function. That would give you a mesh-plot of all points if you call it with something like:
Then adjust and add contour-plots and other stuff as you whish.
NP on 18 Sep 2019
Hi Bjorn,
Thank you very much for taking the time to try to understand my twisted mind! It makes sense for the mesh functions not to plot my interpolated grid + the original points. Pretty cool that the trimesh one does though. Thank you for the suggestion!

Sign in to comment.

More Answers (0)

Sign in to answer this question.