Shift the z -value of contour plot in Matlab 2014b

跟風遠走 提交于 2019-12-02 08:22:21

问题


I'm trying to make a figure of a surface plot, and beneath the surface I wish to show the contour lines, but I want the contour to be at z = -1 instead of at the default value 0. I found a previous post about this problem here, but when I try the solution the contour is still at z = 0. Maybe it has something to do with the version of MATLAB I'm using, which is 2014b? Any ideas on how to make it work?

The code I tried:

%# plot surface and contour
Z = peaks;
surf(Z), hold on
[~,h] = contourf(Z);       %# get handle to contourgroup object

%# change the ZData property of the inner patches
hh = get(h,'Children');    %# get handles to patch objects
for i=1:numel(hh)
    zdata = ones(size( get(hh(i),'XData') ));
    set(hh(i), 'ZData',-10*zdata)
end

回答1:


So, I couldn't really figure out to do it as proposed in the example I found and posted, but I found a way that works. What I ended up doing was basically this:

figure
hold on
surf(X,Y,Z+1);
contour(X,Y,Z);
zz = get(gca,'ZTick');
set(gca,'ZTickLabel',sprintf('%3.1f\n',zz-1));

This gets me the surf and contour in the same figure, but yields some problems with color mappings.




回答2:


I figured out how to solve the problem with color mappings the user Kine faced. Note: I've done the following code on MATLAB R2015b:

offset = 0.5;
plotHandle = surfc(X1, Y1, Z1);
hold on;
% This line moves the surface up from its original location and increases the space between the surface and the contour plot
plotHandle(1).ZData = plotHandle.ZData + offset;
% However in doing so the color mappings are changed. So, the line below restores these mappings
plotHandle(1).CData = plotHandle.CData - offset;

% This line changes fills the contour plot
plotHandle(2).Fill = 'on';
grid on;

% The following lines draw critical areas on the contour line, as it was more readable in my case
axisHandle = gca;
ZHeight = axisHandle.ZLim(1);
plot3(X2, Y2, ZHeight, 'o', 'MarkerSize', 10, 'LineWidth', 1, 'Color', 'k', 'MarkerFaceColor', 'm');
plot3(Y2, X2, ZHeight, 'o', 'MarkerSize', 10, 'LineWidth', 1, 'Color', 'k', 'MarkerFaceColor', 'm');

hold off



回答3:


I got the same problem. And finally, I got the contourf on plane Z=-10. My MATLAB version is

MATLAB Version: 8.5.0.197613 (R2015a)

hope the codes work 4 you

clear all
clc

[X,Y,Z] = peaks;

[~,hContour] = contourf(X,Y,Z,20,'edgecolor','none');

hContour.ContourZLevel = -10; % set the contour's Z position

view(44,30)

colormap(jet)


来源:https://stackoverflow.com/questions/27204011/shift-the-z-value-of-contour-plot-in-matlab-2014b

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!