Plot contour with "concave" XY coordinates
Afficher commentaires plus anciens
Hello
I have an array of XYZ coordinates and want to plot them using contourf, so I followed this guide: https://de.mathworks.com/matlabcentral/answers/814925-plots-using-contourf-in-matlab
This is the result. The red dots are a scatterplot of the XY coordinates and beyond the concave border of my data there is extrapolated garbage.
I assume this happens because of linspace, but what could I use instead? I could determine the Y border for every X and set the Z coordinate corresponding to each Y past that border to NaN, but there must be a better way.

This is the code and I have also attached the data points to this post:
clear
close all
load(websave('myFile', 'https://de.mathworks.com/matlabcentral/answers/uploaded_files/1346319/data.mat'))
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);
hold on
contourf(Xm,Ym,Zm,10);
scatter(X,Y,'red');
Please lend me your brain power, for mine is weak.
Réponse acceptée
Plus de réponses (1)
Bjorn Gustavsson
le 5 Avr 2023
Modifié(e) : Cris LaPierre
le 5 Avr 2023
NOTE: edited so that the code runs here and displays the results CL
The problem happens because griddata (and both scatteredInterpolant and triscatteredInterp) produce a triangulation with a convex perimeter.
If you switch to use an explicit delaunay-triangulation and use tricontour instead you might evade that filling between your concave boundary and the default convex one. But that might require some handcraft-work to get right.
load data.mat
tri = delaunay(Y,X);
plot(X,Y,'ro')
hold on
for i1 = 1:numel(X)
text(X(i1)+50,Y(i1)+5,num2str(i1))
end
% manual identification of the concave part of the perimeter
idxConcave = [70 78 85 91 97 102 107 112 116 120 124 128 131 134];
tri3 = tri;
% if all three corners of a triangle belong to the concave perimeter
for i1 = size(tri3):-1:1
idxRM(i1) = numel(intersect(tri3(i1,:),idxConcave))==3;
end
% I assume that you want them gone
tri3(idxRM,:) = [];
triplot(tri3, X, Y,'r')
tricontour(tri3,X,Y,Z,linspace(min(Z),max(Z),8))
pause(2)
clf
tricontour(tri3,X,Y,Z,linspace(min(Z),max(Z),8))
hold on
plot(X,Y,'k.')
Here I've used the tricontour-function (contour-plot-for-scattered-data by Duane Hanselman), hopefully you can find a similar tool that make filled contours.
HTH
1 commentaire
Dennis Weber
le 6 Avr 2023
Catégories
En savoir plus sur Contour Plots dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


