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

5 vues (au cours des 30 derniers jours)
carlos lopez
carlos lopez le 23 Juil 2025
Modifié(e) : Matt J le 25 Juil 2025
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 commentaires
DGM
DGM le 23 Juil 2025
Modifié(e) : DGM le 24 Juil 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:

Connectez-vous pour commenter.

Réponse acceptée

Matt J
Matt J le 24 Juil 2025
Modifié(e) : Matt J le 24 Juil 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 commentaires
carlos lopez
carlos lopez le 24 Juil 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 le 25 Juil 2025
Modifié(e) : Matt J le 25 Juil 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).

Connectez-vous pour commenter.

Plus de réponses (1)

Walter Roberson
Walter Roberson le 24 Juil 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);
  1 commentaire
carlos lopez
carlos lopez le 24 Juil 2025
This seems to be also a straightforward solution. Thank you for the tip.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Contour Plots dans Help Center et File Exchange

Tags

Produits


Version

R2006a

Community Treasure Hunt

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

Start Hunting!

Translated by