- 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
조회 수: 4 (최근 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!