MATLAB: Plotting contours from a shapefile on top of raster data (which were plotted using surf)

Mapping Toolboxplottingshapefile

I would like to plot subcatchment contours (which I have read using shaperead) on top of a precipitation map I have plotted using surf. If I only plot the contours they appear in the figure (e.g. if I put a breakpoint in the code below before plotting the precipitation maps), however if I first plot the precipitation map, the contours don't show. Does anybody have an idea, what the problem is? Many thanks, Doris
This is the code I use:
figure1 = figure;
axes1 = axes('Parent',figure1);
xlim(axes1,[xmin xmax]);
ylim(axes1,[ymin ymax]);
caxis([0 200]);
hold(axes1,'all');
% plot subbasin contours

plot([S.X],[S.Y], 'k');
% plot precipitation map
surf(dem_x,dem_y,precip_map);
shading flat
view(0,90)
hold on;
% plot subbasin contours
plot([S.X],[S.Y], 'k');

Best Answer

You will using run in to problems when you plot 2D objects in a scene that has 3D objects. It is safer use plot3d() and supply specific Z coordinates.
You should specifically parent your plot() and surf() against axes1 as otherwise you cannot be sure where they will end up.
With your view(0,90) you are plotting lines and surface objects in the same plane (Z=0). The OpenGL renderer does not use order-of-drawing in such cases: it has specific rules about which is to be drawn first, and some graphics cards get the rules exactly backwards. To prevent this problem from happening, either use a different renderer, or give your lines a Z coordinate that will put them "in front" of the surface if you want to see them on top of the surface. But keep in mind that the Z values in your surface range up to max(precip_map(:)) so if you want something "in front" of that, your Z for the lines would have to exceed that.
If you are using surf() and rotating the view to be from above, you may wish to use pcolor() instead.