如何修改下面的代码以使用for循环绘制轮廓线?

问题描述 投票:0回答:1

下面的代码在Zlevel指定的Z轴上的不同位置绘制多个轮廓图。但是,我有多个感兴趣的Z数据点,因此我想使用一个for循环。

Zlevel=[0 1];

figure(1)
hold on 
[~,h1]=contourf(xx,yy,zz(:,:,1)); h1.ContourZLevel=Zlevel(1);
hold on 
[~,hh1]=contour(xx,yy,yy); hh1.ContourZLevel=h1.ContourZLevel;  
hold on 
[~,h2]=contourf(xx,yy,zz(:,:,2)); h2.ContourZLevel=Zlevel(2);
hold on 
[~,hh2]=contour(xx,yy,yy);hh2.ContourZLevel=h2.ContourZLevel;
hold off

我以为我可以有这样的东西:

figure(1); hold on;
for i=1:length(Zlevel)
    [~,h(i)]=contourf(xx,yy,zz(:,:,i)); h(i).ContourZLevel=Zlevel(i); 
    hold on
    [~,hh(i)]=contour(xx,yy,yy); hh(i).ContourZLevel=Zlevel(i);
    hold on 
end
hold off

我已经尝试过了,但是无法正常工作。我可能不了解matlab的对象处理。因此,如果有人可以帮助我并向我解释为什么我不能做我想做的事情并指出正确的方向,我将不胜感激!

谢谢!

matlab matlab-figure contourf
1个回答
0
投票

我不太清楚,为什么每个图需要两次调用contourf,因为secodn应该只会在图像上产生一些不良线条。...

这是您想要的吗?

% dummy data
[xx,yy,zz] = peaks;
zz(:,:,2)=-zz(:,:,1);
zz(:,:,3)=-2*zz(:,:,1);
zz(:,:,4)=2*zz(:,:,1);



Zlevel=[0 0.25 0.5 1];

figure(1)
hold on
for ii=1:length(Zlevel)
    [~,h(ii)]=contourf(xx,yy,zz(:,:,ii)); h(ii).ContourZLevel=Zlevel(ii);

end
view(3)

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.