2014-07-07 52 views
4

我想知道如何繪製多個,2D等高線圖中的z軸間隔開,在3D圖像這樣:情節多個2D等高線圖

blah

+0

您可能要查找的命令是[contourslice](http://www.mathworks.com/help/matlab/ref/contourslice.html)。請參閱文檔以作進一步參考 – CitizenInsane

+0

@CitizenInsane:該功能旨在可視化體數據,我不認爲OP意味着在這裏做到這一點。 – Amro

+0

@Amro Humm可能很容易將數據視爲體積數據。你的解決方案很好。 – CitizenInsane

回答

6

注意:這個答案的第一部分是爲HG1圖形。如果您使用MATLAB R2014b或更高版本(HG2),請參閱第二部分。


HG1:

contour函數在內部創建許多patch對象,並且返回它們作爲組合hggroup object。因此,我們可以通過將Z尺寸移動到所需的水平(默認輪廓在z = 0處顯示)來設置所有貼片的ZData

下面是一個例子:

[X,Y,Z] = peaks; 
surf(X, Y, Z), hold on  % plot surface 

[~,h] = contour(X,Y,Z,20); % plot contour at the bottom 
set_contour_z_level(h, -9) 
[~,h] = contour(X,Y,Z,20); % plot contour at the top 
set_contour_z_level(h, +9) 

hold off 
view(3); axis vis3d; grid on 

multiple_contour_plots

下面是上述用於set_contour_z_level功能的代碼:

function set_contour_z_level(h, zlevel) 
    % check that we got the correct kind of graphics handle 
    assert(isa(handle(h), 'specgraph.contourgroup'), ... 
     'Expecting a handle returned by contour/contour3'); 
    assert(isscalar(zlevel)); 

    % handle encapsulates a bunch of child patch objects 
    % with ZData set to empty matrix 
    hh = get(h, 'Children'); 
    for i=1:numel(hh) 
     ZData = get(hh(i), 'XData'); % get matrix shape 
     ZData(:) = zlevel;    % fill it with constant Z value 
     set(hh(i), 'ZData',ZData);  % update patch 
    end 
end 

HG2:

上述解決方案不再以R2014b開始。在HG2中,contour objects不再具有任何圖形對象作爲子項(Why Is the Children Property Empty for Some Objects?)。

幸運的是,有一個簡單的修復,隱藏輪廓屬性稱爲ContourZLevel。 您可以瞭解更多無證等高線圖的定製herehere

所以在前面的例子簡直變成:

[X,Y,Z] = peaks; 
surf(X, Y, Z), hold on  % plot surface 

[~,h] = contour(X,Y,Z,20); % plot contour at the bottom 
h.ContourZLevel = -9; 
[~,h] = contour(X,Y,Z,20); % plot contour at the top 
h.ContourZLevel = +9; 

hold off 
view(3); axis vis3d; grid on 

contours


,在適用於所有版本的另一種解決方案是「父」的曲線到hgtransform對象,變換使用一個簡單的z-translation。像這樣:

t = hgtransform('Parent',gca); 
[~,h] = contour(X,Y,Z,20, 'Parent',t); 
set(t, 'Matrix',makehgtform('translate',[0 0 9])); 
+0

謝謝!這正是我想要繪製的那種形象。 有沒有辦法調整間距/視角,使輪廓不重疊?(我正在繪製輪廓的三個級別,如圖,但是每個輪廓與當前透視圖重疊) – Krish

+0

您可以[交互式旋轉](http://www.mathworks.com/help/matlab/creating_plots /rotate-3d-interactive-rotation-of-3-d-views.html)3D視圖,或以['view(az,el)'](http://www.mathworks.com/ help/matlab/ref/view.html):http://www.mathworks.com/help/matlab/visualize/setting-the-viewpoint-with-azimuth-and-elevation.html。您也可以[控制相機](http://www.mathworks.com/help/matlab/visualize/view-control-with-the-camera-toolbar.html)以獲得更大的靈活性 – Amro

+0

我已經更新了答案展示HG1和HG2的解決方案 – Amro