I want to clip a contour output to the interior of a closed polygon

4 views (last 30 days)
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 Comments
DGM
DGM on 23 Jul 2025
Edited: DGM on 24 Jul 2025
Consider a closed contour line (a loop) which crosses the polygonal boundary. Which should happen?
  1. The contour line ends where it contacts the boundary, leaving the contour open
  2. The contour line meets and follows the boundary, leaving the contour closed
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:

Sign in to comment.

Accepted Answer

Matt J
Matt J on 24 Jul 2025
Edited: Matt J on 24 Jul 2025
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 Comments
carlos lopez
carlos lopez on 24 Jul 2025
Thank you all for the quick and detailed answers, from you and the other experts. I'll probably follow the straightforward suggestion: once each contour line has been extracted, simply replot it as a line. I will add the labels before that, so I can represent the contour with labels as a set of lines and text.
Matt J
Matt J on 25 Jul 2025
Edited: Matt J on 25 Jul 2025
You're welcome, but please Accept-click the solution you are going with (it sounds to me like you are going with this one).

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 24 Jul 2025
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);

Categories

Find more on Contour Plots in Help Center and File Exchange

Tags

Products


Release

R2006a

Community Treasure Hunt

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

Start Hunting!