2D contour plot considering the boundary of data
조회 수: 13(최근 30일)
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 2021년 8월 15일
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';