- The contour line ends where it contacts the boundary, leaving the contour open
- The contour line meets and follows the boundary, leaving the contour closed
I want to clip a contour output to the interior of a closed polygon
3 次查看(过去 30 天)
显示 更早的评论
Let's assume that I can create a contour plot for a square region. However, I do not want the lines to spill outside a certain polygon (completely included in the square), so I want to clip them. The graphics object for the contour is not a set of lines, but something called hggroup. One of the fields is ContourMatrix, which holds a description of the lines in a two-row matrix. If I decode the matrix, clip the lines, rebuild the contours by create a (likely suitable) two row matrix and finally store as the new ContourMatrix field, the system complains saying that such field is read only. Is there a way to operate over the individuals lines of the contour plot?
2 个评论
DGM
2025-7-23
编辑:DGM
2025-7-24
Consider a closed contour line (a loop) which crosses the polygonal boundary. Which should happen?
FWIW, there are a number of ways to get the curve data from a contour plot, but they're all fairly cumbersome. The first output argument contains all the curves in a blockwise format. Each block is arranged in the format [thislevel thisxvalues; blocklength thisyvales]
% an example
z = peaks(100);
[C,hc] = contour(z);
contourlevels = hc.LevelList;
% get vertex sequences from contours
% Vc is a cell array of [x y] vertex lists describing individual curves,
% each column in Vc contains the curves for a particular level
for kl = 1:numel(contourlevels) % index over each level
startidx = find(C(1,:) == contourlevels(kl)); % start of each curve
for kc = 1:numel(startidx) % index over each curve in this level
blockstart = startidx(kc); % the starting index for this block
lenv = C(2,blockstart); % the length of this curve + 1
Vc{kc,kl} = C(:,blockstart+(2:lenv)).'; % C is [level, x; length, y]
end
end
I'm prety sure there are FEX tools to do this more conveniently.
EDIT: possibly relevant:
采纳的回答
Matt J
2025-7-24
编辑:Matt J
2025-7-24
Since you have extracted the contour line coordinate data and modified them, you could just replot them as line plots instead of contour plots.
If you want the clipped contour lines to follow the boundary of the polygon, as @DGM hypothesizes, then this will be your only option. It will not be possible for you to represent such a clipping in contour plot form because the clipping will cause different isocontours to intersect at the polygon boundary. However, it is not permissible for different isocontour lines to intersect in a contour plot, because the points of intersection would then have a non-unique level associated with them.
2 个评论
更多回答(1 个)
Walter Roberson
2025-7-24
Create a polyshape() with the boundaries of the enclosing polygon. Use isinterior to test the x, y coordinate pairs implied by ndgrid() of the contour grid points. Now take
NewDataMatrix = YourContourDataMatrix;
NewDataMatrix(~PointsAreInterior) = nan;
contour(XGrid, YGrid, NewDataMatrix);
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Contour Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!