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

4 visualizaciones (últimos 30 días)
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 comentarios
DGM
DGM el 23 de Jul. de 2025
Editada: DGM el 24 de Jul. de 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:

Iniciar sesión para comentar.

Respuesta aceptada

Matt J
Matt J el 24 de Jul. de 2025
Editada: Matt J el 24 de Jul. de 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 comentarios
carlos lopez
carlos lopez el 24 de Jul. de 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 el 25 de Jul. de 2025
Editada: Matt J el 25 de Jul. de 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).

Iniciar sesión para comentar.

Más respuestas (1)

Walter Roberson
Walter Roberson el 24 de Jul. de 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);

Categorías

Más información sobre Contour Plots en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2006a

Community Treasure Hunt

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

Start Hunting!

Translated by