MATLAB Answers

Contour plot ignores a set of data points

38 views (last 30 days)
James
James on 28 May 2020
Commented: James on 28 May 2020
Hello,
I'm constructing a contour plot using location (longitude and latitude) and respective depth data. However, the output contour plot seems to ignore (or interpolate in a way that is not intended) a set of data points. This makes the result undesirable. Is there any thing that I can do to fix this?
Following is the piece of code that I use to plot the contour plot.
% reading the bathy data
data = xlsread('datafile.xlsx');
lon = data(:,1); % Longitude
lat = data(:,2); % Latitude
alt = data(:,3); % Altitude
[xq,yq] = meshgrid(min(lon):.001:max(lon), min(lat):.001:max(lat));
[x,y,z] = griddata(lon,lat,alt,xq,yq, 'cubic');
contourf(x,y,z);
I have attached the figues for reference.
  • Data_Points.png (Locations of the data points)
  • Overlay.png (Result contour plot is overlaid on Google Earth)
  • Overlay2.png (Result contourf plot)
  12 Comments
James
James on 28 May 2020
Honestly, I had no idea about these approaches. Thanks a lot, and this is going to take some time for me to learn and play around because I have not tried these before :)
I will comment here again with my progress. Thanks again, Bjorn! You made the day!

Sign in to comment.

Accepted Answer

John D'Errico
John D'Errico on 28 May 2020
I've moved what was a comment into an answer, then added a bit...
A very simple example of what I described is shown here:
px = [0, 1, .5, .49, 0];
py = [0, 0, sqrt(3), sqrt(3)-.5, 0];
ps = polyshape(px,py);
[x,y] = meshgrid(linspace(0,1,100),linspace(0,1.8,200));
z = x.^2 + y.^2;
inp = ps.isinterior(x(:),y(:));
z(~inp) = NaN;
H = contourf(x,y,z);
hold on
HB = plot(ps);
HB.FaceColor = 'none';
The edges are coarsely stairstepped, since I used only a fairly coarse mesh. Also note that the result lives ENTIRELY inside the polygonal region. So the steps intrude into the polyshape, because contourf will not contour any part of the grid that even touches a NaN. A finer mesh would reduce the stairstepping artifacts.
I defined z there using a simple computation, but nothing would have prevented me from using a tool like griddata or scatteredInterpolant if I had real data.
Is this a bit of a hack? Well, sadly, yes. That is because griddata still is using data from acrsss a hole to infer the shape of that surface in some region. I just deleted what griddata did in those regions, so contourf did not see the stuff I wanted hidden. The point is, griddata is still doing what is technically a bad thing, because it does not understand what you want. And that MAY introduce artifacts into the result.
As I said, artifacts might arise. Consider a randomly u-shaped region. In this case, the region was just based on some quick clicks of the mouse using ginput. Then I created a polyshape, based on the perimeter polygon, with some randomly generated data inside of it.
My fear is that what happens in one wing of that u-shaped region can cross the peninsula between those wings to influence the shape in the other wing. This could be made more significant due to the use of cubic interpolation in griddata. That would not be true in the case where the surface is constructed from a triangulation that lives inside only the polygon. At the same time, the only tools I have to build that surface on a general non-convex triangulated region would require some tools that are now fairly outdated.
The final issue mentioned is how to extract the polygon itself, if all you have are the scattered points. This is somewhat difficult to do via an automatic scheme, because all scattered data has holes between EVERY pair of points. It seems as if the data points around the perimeter were fairly closely spaced. So you might try to use some sort of intelligent scheme to build the polygon. Personally, unless you are going to do this more than a few times, I might just carefully draw it in using a mouse/tablet and ginput.
If you can live with this simplistic solution, then it may be a good choice for you.
  1 Comment
James
James on 28 May 2020
Perfect! I will use this approach, together with what Bjorn suggested in his comment. (That will be required to get the coordinates needed for the polyshape)
Thank you so much for the detailed explaination. I'm really grateful! :)

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by