Matlab - surf and contour3, clipping order?

2020-04-16 02:54发布

问题:

I am plotting data as a surface in matlab. I have three data matrices, x,y,z. The values of z may not be outside the range 0~1.

I generate plots with the following:

surf(x,y,z);
[c,h] = contour3(x,y,z,'LevelList',[0 : 0.1 : 1],'Color','k');
clabel(c,h,[0 : 0.1 : 1]);

I also do some modifications to the surface, such as setting shading interp.

As you can see, the result image clips the contours with the underlying surface. How can I ensure that the contour and labels are plotted above the surface?

回答1:

After some digging in the doc, I have found the best solution.

The clipping order is specified at the axes level.

A complete MWE to have the contours always on top of the surface is below:

fig = figure;
ax = get(gca);
ax.SortMethod = 'childorder'; % this is the important line
surf(x,y,z);
[c,h] = contour3(x,y,z,'LevelList',[0 : 0.1 : 1],'Color','k');
clabel(c,h,[0 : 0.1 : 1]);


回答2:

You can try working on the properties of the contour patches: increase the linewidth of the patch' edge: the default value is 0.5: a linewidth of 1 or 1.5 should be enough.

On the same way you can set the properties of the lavels generated by clabel: you can set the font size and font weight to make them more visible. Also you can set the number of label to be added by specifying the labelspacing property.

An interesting option could also be to manually set the labels: this can be done by specifying the manaul property in the clabel call.

In the following you find an example based on the peaks surface:

[x,y,z]=peaks
surf(x,y,z);
shading interp
hold on
[c,h] = contour3(x,y,z,[-10:1:10]);
set(h(:),'linewidth',1,'edgecolor','k')
clabel(c,h,[-10:1:10],'fontsize',9,'fontweight','bold','rotation',0,'labelspacing',99);
% clabel(c,h,'manual','fontsize',9,'fontweight','bold','rotation',0);

Hope this helps.