Clear Filters
Clear Filters

How to fill contours in a contour matrix

4 views (last 30 days)
Otis
Otis on 9 Sep 2014
Commented: Otis on 11 Sep 2014
Yeah, I know, I could use contourf, but I want to draw the contours generated by contourc on another plot. What I need help with is how to close the contours, in particular those at the edge of the data.
If you run this code, you will see what I mean. How to properly close the contour at the lower left corner and the one at the top.
data = [ ...
9 9 9 9 1 1 1 1 1 1 1 1 1 1 1 1; ...
9 9 9 9 1 1 1 1 1 1 1 1 1 1 1 1; ...
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1; ...
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1; ...
1 1 1 1 1 1 1 9 9 9 1 1 1 1 1 1; ...
1 1 1 1 1 1 1 9 9 9 1 1 1 1 1 1; ...
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1; ...
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1; ...
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1; ...
1 1 1 1 1 1 1 1 9 1 1 1 1 1 1 1; ...
1 1 1 1 1 1 1 9 9 9 1 1 1 1 1 1; ...
1 1 1 1 1 1 9 9 9 9 9 1 1 1 1 1];
figure(1)
clf
subplot(2,2,1)
imagesc(data)
set(gca, 'YDir', 'normal', 'XLim', [1 12], 'YLim', [1 12]);
subplot(2,2,2)
C = contourf(data, 1);
subplot(2,2,3)
kdx = 1;
while (1)
l = C(1,kdx);
n = C(2,kdx);
x = C(1,(kdx+1):(kdx+n-1));
y = C(2,(kdx+1):(kdx+n-1));
plot(x, y); hold on
patch(x, y, l);
kdx = kdx + n + 1;
if (kdx > size(C,2))
break;
end
end
set(gca, 'XLim', [1 12], 'YLim', [1 12]);
Thanks so much Bill
  3 Comments
Otis
Otis on 9 Sep 2014
I am using contourc to generate contours in a set of data. I want to take those contours and draw them over top an area in another plot of a completely different set of data. I will adjust the x,y values accordingly, and I want to have the option to draw them either as lines or as filled patches. As you can see from my simple example, either the contour matrix output by contourc/contourf does not contain enough information to draw the contours properly (subplot(2,2,3) does not match subplot(2,2,2)) or I'm missing something.
Otis
Otis on 11 Sep 2014
I had to punt on this problem because of time, I needed to get something done today. But I still want to understand how the contour matrix, C, computed by contourc, is used to draw the boxes in subplot(2,2,2) and not subplot(2,2,3)
>> disp(C')
1.0000 1.0000
NaN NaN
5.0000 11.0000
11.5000 12.0000
11.0000 11.5000
10.5000 11.0000
10.0000 10.5000
9.5000 10.0000
9.0000 9.5000
8.5000 10.0000
8.0000 10.5000
7.5000 11.0000
7.0000 11.5000
6.5000 12.0000
5.0000 11.0000
10.0000 6.5000
10.5000 6.0000
10.5000 5.0000
10.0000 4.5000
9.0000 4.5000
8.0000 4.5000
7.5000 5.0000
7.5000 6.0000
8.0000 6.5000
9.0000 6.5000
10.0000 6.5000
5.0000 6.0000
1.0000 2.5000
2.0000 2.5000
3.0000 2.5000
4.0000 2.5000
4.5000 2.0000
4.5000 1.0000

Sign in to comment.

Answers (1)

Image Analyst
Image Analyst on 9 Sep 2014
I'd do it as an image. See http://blogs.mathworks.com/steve/2009/02/18/image-overlay-using-transparency/ If you only have one level for the contour, as your one example shows (just the "red" level) then you can threshold and get a binary image and overlay the binary image as Steve shows in his blog. If you want outlines, you can use bwboundaries() in the Image Processing Toolbox and then use plot() to plot the boundaries as graphical lines in the overlay above the image.

Categories

Find more on Contour Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!