- Correctly set up the contour plot if your data are truely bound to the shape in fig 2.
- or, use inpolygon to isolate the components of the contour plot that are within the shape in fig 2.
2D contour plot considering the boundary of data
15 views (last 30 days)
Show older comments
Hi,
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:
n=5;
[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.
figure
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?
Thanks
2 Comments
Adam Danz
on 13 Aug 2021
If you attach a mat file with the data and provide the code so we can easily copy-paste it to reproduce the plots, we won't have to imagine what the data are. For example, much of the variation in the controur plot seems to take place within the missing part of the rectangular frame in fig 2 so I have no idea where that's coming from.
My initial thoughts are
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.
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';
patch(ff)
axis equal
0 Comments
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);
0 Comments
See Also
Categories
Find more on Contour Plots 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!