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.
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?
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';