How to fill contours in a contour matrix
1 次查看(过去 30 天)
显示 更早的评论
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
回答(1 个)
Image Analyst
2014-9-9
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Contour Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!