Extract interpolated data from contourf?
24 次查看(过去 30 天)
显示 更早的评论
Hello,
I have an elevation matrix (xq,yq,zq) that I have refined using meshgrid. I am hoping to extract a continuous 1 m contour, but my data is a little interrupted.
contourf provides a really nice output plot of continuous filled data between the extracted 1 m contour vertices. That's exactly what I am after. However, when I extract the contourline x,y data, I only obtain the vertices and not the data interpolated in between the vertices.
How do I extract the interpolated data shape that contourf plots? My goal is to fill in the gaps between the black and red lines shown in the image, tracking along the border of the blue data.
V = [1,1]
[M, h] = contourf(xq,yq,zq, V); %% really nice plot of contour (black) + filled/interpolated data (blue). See Fig. 1/
contourTable = getContourLineCoordinates(M);
plot(contourTable_20170918_1.X, contourTable_20170918_1.Y, 'r.', 'MarkerSize', 10) %% only contour vertices (red). See Fig. 2.
---> How do I extract the data between the black (Fig. 1) and red (Fig. 2) data, following the blue data border from contourf (Fig. 1)?

0 个评论
回答(1 个)
Star Strider
2023-9-12
编辑:Star Strider
2023-9-12
The contour functions return (x,y) matrices for each contour, and in all likelihood, the ‘x’ values are not the same for both contours, so it may be necessary to use interp1 to interpolate the ‘y’ vectors to a common reference. (This is actually straightforward, although you may want to do this with the ‘y’ vectors and interpolate the ‘x’ vectors instead in this instance.) After that, use either trapz or cumtrapz to calculate the areas, depending on what you want for the result.
The actual data and code would be necessary to provide a specific solution.
EDIT — (12 Sep 2023 at 15:35)
That might go something like this with your contours —
x = linspace(0,10);
y = linspace(0, 20);
[X,Y] = ndgrid(x,y);
Z = exp(-((X-2).^2+0.1*(Y-10).^2)/50);
figure
surfc(X,Y,Z)
xlabel('X')
ylabel('Y')
zlabel('Z')
figure
[M,h] = contourf(X,Y,Z, 'ShowText',1);
xlabel('X')
ylabel('Y')
Lvls = h.LevelList
Extract = Lvls([4 5])
for k = 1:numel(Extract)
idx = find(M(1,:)==Extract(k));
Len = M(2,idx);
rngv = idx+1:idx+Len;
xv{k} = M(1,rngv);
yv{k} = M(2,rngv);
end
vi = linspace(yv{k}(1), yv{k}(end), 250); % Common Interpolation Vector
for k = 1:numel(Extract)
xi(k,:) = interp1(yv{k}, xv{k}, vi); % Interpolated 'x' Vector
Area(k) = trapz(vi, xi(k,:));
end
AreaBetweenContours = diff(Area);
fprintf('\nThe Area between contour %.2f and %.2f is %.2f\n', Extract, AreaBetweenContours)
.
4 个评论
Star Strider
2023-9-12
I am not certain what you want.
My latest guess —
x = linspace(0,10);
y = linspace(0, 20);
[X,Y] = ndgrid(x,y);
Z = exp(-((X-2).^2+0.1*(Y-10).^2)/50);
figure
surfc(X,Y,Z)
xlabel('X')
ylabel('Y')
zlabel('Z')
figure
[M,h] = contourf(X,Y,Z, 'ShowText',1);
xlabel('X')
ylabel('Y')
Lvls = h.LevelList
Extract = Lvls([4 5])
for k = 1:numel(Extract)
idx = find(M(1,:)==Extract(k));
Len = M(2,idx);
rngv = idx+1:idx+Len;
xv{k} = M(1,rngv);
yv{k} = M(2,rngv);
Q = [xv{k}(1) xv{k}(end); yv{1}(1) yv{k}(end)]
end
vi = linspace(yv{k}(1), yv{k}(end), 250); % Common Interpolation Vector
for k = 1:numel(Extract)
xi(k,:) = interp1(yv{k}, xv{k}, vi); % Interpolated 'x' Vector
Area(k) = trapz(vi, xi(k,:));
end
AreaBetweenContours = diff(Area);
fprintf('\nThe Area between contour %.2f and %.2f is %.2f\n', Extract, AreaBetweenContours)
xedge1 = [xv{1}(1) xv{2}(1)]; % Upper Edge
yedge1 = [yv{1}(1) yv{2}(1)];
xedge2 = [xv{1}(end) xv{2}(end)]; % Lower Edge
yedge2 = [yv{1}(end) yv{2}(end)];
figure
patch([xv{1} flip(xv{2})], [yv{1} flip(yv{2})], 'b', 'FaceAlpha',0.5)
hold on
plot(xv{1}, yv{1})
plot(xv{2}, yv{2})
plot(xedge1, yedge1)
plot(xedge2, yedge2)
hold off
axis([-1 11 -1 21])
title('Filled Extracted Contour')
.
另请参阅
类别
在 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!







