2D contour plot considering the boundary of data

20 views (last 30 days)
I have some discrete data, calling x and y as the coordinates of points and its intensity as of each point as z. I have stored them in 3 vectors namely, testX, testy and testZ. i have tried to make a 2D contour plot by this code:
[X,Y] = meshgrid(linspace(min(testX),max(testX),n), linspace(min(testY),max(testY),n));
Z = griddata(testX,testY,testZ,X,Y);
% Plot a graph.
contourf(X,Y,Z,30,'LineColor', 'none');
and the contour I got is shown in figure 1. The problem is the boundray of my data should not be a rectangular as shown in figure 1 and it should be as I plotted in figure 2.
Could you please let me know how to make a contour plot that consider the boundray of data?
John D'Errico
John D'Errico on 13 Aug 2021
You have discrete data points. That means there are spaces BETWEEN the data. How does griddata know that the concave hole along one edge is not just another space between data points to interpolate across? Computers cannot read your mind. So when it interpolates across that domain, it just sees a simple rectangle.
The best solution is to use code that will understand the geometry of a non-convex domain, then generating a contour plot on a triangulated region. The problem is, MATLAB does not provide that tool. There are a couple of tricontour tools to be found on the file exchange, but you would need to generate a triangulation of that domain.
A somewhat simpler alternative is to use the triangulation as found, but to then kill off the parts of those contours that fall outside the domain of interest. For this, you need to learn nothing more than how to generate the contours using contourc, and then to use inpolygon (or a polyshape) to zap that which lies outside of the domain.

Sign in to comment.

Answers (2)

darova on 15 Aug 2021
Use initmesh to mesh
[x,y,z] = peaks(20);
t = linspace(0,-pi,20);
[xr,yr] = pol2cart(t,2); % round part
x1 = [-3 -3 3 3 xr]; % square coordinates
y1 = [3 -3 -3 3 yr+3]; % square coordinates
gd = [2;length(x1);x1(:);y1(:)]; % geom description of the entire curve
dl = decsg(gd); % decomposition
[p,e,t] = initmesh(dl); % build a mesh
z2 = griddata(x,y,z,p(1,:),p(2,:)); % interpolate Z coord
ff.faces = t(1:3,:)'; % represent each triangle (mesh) as separate face
ff.vertices = p'; % points
ff.facevertexcdata = z2(:); % colors
ff.facecolor = 'interp';
ff.edgecolor = 'none';
axis equal

Chunru on 13 Aug 2021
[x, y, z] = peaks(40);
h = pcolor(x, y, z);
h.EdgeColor = 'none';
% Now plot a mask
hold on
ps = polyshape([-1 -1 1 1], [3 2 2 3]); % Specify your shape
plot(ps, 'FaceColor', 'k', 'FaceAlpha', 1);


Find more on Triangulation Representation in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!