2D contour plot considering the boundary of data

조회 수: 13(최근 30일)
arzedaily4121 2021년 8월 12일
답변: darova 2021년 8월 15일
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?
  댓글 수: 2
John D'Errico
John D'Errico 2021년 8월 13일
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.

댓글을 달려면 로그인하십시오.


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';
axis equal

Chunru 2021년 8월 13일
[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);

Community Treasure Hunt

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

Start Hunting!

Translated by