MATLAB: How to place a quiver plot on top of a surface plot

MATLAB

When I place a quiver plot on a 2-D surface plot (pcolor), resizing or printing the figure window causes the quiver plot to disappear. I would like to place a quiver plot on top of a surface plot.

Best Answer

The quiver plot disappears because both the surface plot and quiver plot are located in the XY-plane (Z = 0) and the renderer is set to 'painters'. You can avoid this issue by changing the renderer as follows:
set(gcf, 'renderer', 'zbuffer')
If you would like to keep the renderer to 'painters', you can place the surface plot at a -Z offset. For example:
xord = -2:.2:2;
yord = -2:.2:2;
[x,y] = meshgrid(xord,yord);
z = x .* exp(-x.^2 - y.^2);
[px,py] = gradient(z,.2,.2);
quiver(x,y,px,py)
hold on
h = pcolor(x,y,z);
set(h,'ZData',-1+zeros(size(z))) % Move the surface plot to Z = -1
colormap(gray)
shading interp