连接等高线的极值点
Connect extremal points of contour lines
我正在尝试在 matlab 中标记并连接等高线图中的最大点。
如果我举这个简单的例子:
x = -2:0.2:2;
y = -2:0.2:3;
[X,Y] = meshgrid(x,y);
Z = X.*exp(-X.^2-Y.^2);
contour(X,Y,Z,'ShowText','on')
下面给出了轮廓曲线,黑线和红圈是我想要绘制的。
我尝试搜索 Z 中每一行的最大值索引,然后绘制相应的 X 和 Y 值。
这在我原来的问题中没有给我正确的值。
我正在寻找的是是否有一种方法可以自动从 figure/data 中提取这些值。
可以从对 contour
. The first returned value is the contour matrix 的调用中提取轮廓线的数据,可以对其进行解析以提供每条轮廓线的点。那么找到 "maximum" 点只是后勤问题,你似乎真的是指轮廓上具有最大 y
值的点:
x = -2:0.2:2;
y = -2:0.2:3;
[X,Y] = meshgrid(x,y);
Z = X.*exp(-X.^2-Y.^2);
M = contour(X,Y,Z,'ShowText','on');
inow = 1;
maxpoints = [];
while inow < size(M,2)
% column at inow contains level value and number of points in contour line
level = M(1,inow);
nvals = M(2,inow);
% extract coordinates of the corresponding contour line
xvals = M(1,inow+1:inow+nvals);
yvals = M(2,inow+1:inow+nvals);
% find max y value along the contour
[ymax, ind] = max(yvals);
xmax = xvals(ind);
maxpoints(end+1,:) = [xmax, ymax];
% increment index
inow = inow + nvals + 1;
end
% filter points as necessary
inds = maxpoints(:,1) > 0; % contours to the right
keeppoints = maxpoints(inds,:);
% plot them
hold on;
plot(keeppoints(:,1),keeppoints(:,2),'o-');
我们构建的maxpoints
数组大小为[ncontours, 2]
,包含"maximum"个点的坐标。如果您只想使用一些可见的轮廓,则需要过滤这些。结果如下:
如您所见,线条的精度仅与等高线本身的精度一样好。您可以通过使数据更密集并因此使轮廓线更平滑来提高准确性。上面在 meshgrid
调用中使用 0.02
步骤而不是 0.2
:
我正在尝试在 matlab 中标记并连接等高线图中的最大点。
如果我举这个简单的例子:
x = -2:0.2:2;
y = -2:0.2:3;
[X,Y] = meshgrid(x,y);
Z = X.*exp(-X.^2-Y.^2);
contour(X,Y,Z,'ShowText','on')
下面给出了轮廓曲线,黑线和红圈是我想要绘制的。
我尝试搜索 Z 中每一行的最大值索引,然后绘制相应的 X 和 Y 值。 这在我原来的问题中没有给我正确的值。
我正在寻找的是是否有一种方法可以自动从 figure/data 中提取这些值。
可以从对 contour
. The first returned value is the contour matrix 的调用中提取轮廓线的数据,可以对其进行解析以提供每条轮廓线的点。那么找到 "maximum" 点只是后勤问题,你似乎真的是指轮廓上具有最大 y
值的点:
x = -2:0.2:2;
y = -2:0.2:3;
[X,Y] = meshgrid(x,y);
Z = X.*exp(-X.^2-Y.^2);
M = contour(X,Y,Z,'ShowText','on');
inow = 1;
maxpoints = [];
while inow < size(M,2)
% column at inow contains level value and number of points in contour line
level = M(1,inow);
nvals = M(2,inow);
% extract coordinates of the corresponding contour line
xvals = M(1,inow+1:inow+nvals);
yvals = M(2,inow+1:inow+nvals);
% find max y value along the contour
[ymax, ind] = max(yvals);
xmax = xvals(ind);
maxpoints(end+1,:) = [xmax, ymax];
% increment index
inow = inow + nvals + 1;
end
% filter points as necessary
inds = maxpoints(:,1) > 0; % contours to the right
keeppoints = maxpoints(inds,:);
% plot them
hold on;
plot(keeppoints(:,1),keeppoints(:,2),'o-');
我们构建的maxpoints
数组大小为[ncontours, 2]
,包含"maximum"个点的坐标。如果您只想使用一些可见的轮廓,则需要过滤这些。结果如下:
如您所见,线条的精度仅与等高线本身的精度一样好。您可以通过使数据更密集并因此使轮廓线更平滑来提高准确性。上面在 meshgrid
调用中使用 0.02
步骤而不是 0.2
: