How to close open contours

31 views (last 30 days)
ForHim
ForHim on 19 Oct 2021
Commented: ForHim on 21 Oct 2021
I would like to close the open contours in the attached image. The contour lines just stop at both the top and bottom of the image. I am trying to find a non-image analysis solution, as this plot was created with the contour function using the following syntax:
[C, h] = contour(X, Y, Z, [level_1 level_2 level_3 level_4 level_5]);
I would like it to close with just a horizontal line connecting to the corresponding isoline/color at both the top and bottom. Either a programatic or brute force solution would be ok with me. I would like to have the connected contour plot available as a Contour matrix, which is what the contour function outputs (https://www.mathworks.com/help/matlab/ref/contour.html).
Thanks!

Accepted Answer

DGM
DGM on 19 Oct 2021
The contours are open because that's what describes the data. To close the contours misrepresents the behavior of the data at the edges. If this were elevation data, you would be explicitly fabricating an artifact that describes a vertical surface at the edge of the plot. You can do it if you want.
z = peaks(100);
z = z(:,1:50);
contour(z);
xlim([-10 60])
ylim([-10 110])
clf
z = padarray(z,[1 1],0,'both');
contour(z);
xlim([-10 60])
ylim([-10 110])
  2 Comments
ForHim
ForHim on 20 Oct 2021
Hi! Thank you for replying! I am not worried about adding the artifact. My end goal is to find the union of the areas of the contours, or the total projected 2D area, and I need the contours to be closed to do it.
I tried using the padarray function with the following code, but the attached images were the result. One image shows the whole plot and the other is a zoomed portion of the upper region.
D = padarray(Z, [1 0], 0, 'both');
contour(D, levels);
I'm not sure why it created those additional lines above the yellow lines, though they may not matter in the end since they are also open. I'm also not sure why it created one long line accross the range of x-values to connect the contours, whereas the example you gave only connected the lines within the contour they belonged to (which is what I also want to do). Finally, it created a connection at the top of the plot but not the bottom. Do you know the cause of these issues?
Thank you for your assistance!
ForHim
ForHim on 21 Oct 2021
Hey, I was able to get this to work! I just had to change the second to last 0 to a 1!
Thanks!

Sign in to comment.

More Answers (0)

Categories

Find more on Contour Plots in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!