1

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
Community
  • 1
  • 1
  • Seams really to be an issue of Matlab 2014. I tried it with 2013b and it worked fine. What is the your dimension of `hh`? In my case, `15` objects are obtained. If you get a reasonable number, check the `get(hh(i),'XData')` command, if this gives you the correct results. – Nemesis Nov 29 '14 at 16:29
  • I get: `hh = 0x0 empty GraphicsPlaceholder array.` –  Nov 29 '14 at 16:36
  • Can you try if `hh = findobj(gca,'Type','Patch');` works instead? – Nemesis Nov 30 '14 at 08:45
  • No difference, `hh`is still 0-by-0 –  Nov 30 '14 at 13:28
  • I'm having the same issue. 2014b introduced a lot of changes to their graphics system. For the most part, they seem to be good (since the old one was clunky and would mangle too many things). However, a bunch of old hacks, like this one, no longer seem to work. – lnmaurer Dec 22 '14 at 01:44
  • @inmaurer If it would be "hacks", fine, I wouldn't complain. But even on the [Matlab Blog](http://blogs.mathworks.com/pick/2011/07/15/creating-hatched-patches/) they use code like this, which is not working anymore, since `hh.Children` is `[]`. So how to manipulate the contour now...? How to obtain the patch objects? – embert Mar 14 '15 at 20:19

3 Answers3

0

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.

0

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
0

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)