MATLAB: How to draw contour on the other two planes when using SURFC

contoursliceMATLABotherplaneprojectionslicesurfx-zxzy-zyz

I use SURFC to plot my data(eg. peaks(30)) and the contour on xy plane looks great for knowing the maximum and minimum at some levels. But I also want to have contours on yz plane and zx plane. Is it possible to do that?

Best Answer

The CONTOURSLICE may be an easy way to achieve this when you have the volume data for your surface plot. But if there's no volume data, it'll get more complex to do that. You'll do CONTOUR for each plane by changing the order of the input arguments and then swapping corresponding x/y/zdata of each contour line to achieve the effect. Please refer to the following example.
[X,Y,Z] = peaks(30);
h1=surfc(X,Y,Z);
xmin=-3;xmax=3;ymin=-3;ymax=3;zmin=-10;zmax=10;
axis([xmin xmax ymin ymax zmin zmax])
hold on;
%%contour on yz plane
[C,h]=contour(Y,Z,X);
hpatch = get(h,'Children');
for i = 1:length(hpatch)
ch = hpatch(i);
xdata = get(ch,'Xdata'); %

ydata = get(ch,'Ydata');
set(ch,'Xdata',zeros(size(xdata))+xmax);
set(ch,'Zdata',ydata);
set(ch,'Ydata',xdata);
end
%%contour on xz plane
[C,h]=contour(X,Z,Y);
hpatch = get(h,'Children');
for i = 1:length(hpatch)
ch = hpatch(i);
xdata = get(ch,'Xdata'); %
ydata = get(ch,'Ydata');
set(ch,'Ydata',zeros(size(ydata))+ymax);
set(ch,'Zdata',ydata);
set(ch,'Xdata',xdata);
end
For scatter data, you could also refer to related solution below "Is it possible to draw contour on YZ and ZX planes?".