MATLAB: How to force negative contours to appear as white with contourf

contourcontour plotcontourfMATLAB

Is it possible to force negative contours to appear as white when plotting with contourf such as in the attached graph?
It is not enough to just set negative values in the matrix to 'NaN' or zero, as that results in a fragmented edge near zero, not a clean edge like the attached.

Best Answer

Try this
[X, Y] = meshgrid(linspace(-2,2));
Z = X.^2 + Y.^2 - 2;
figure;
ax = axes();
[~, c] = contourf(X, Y, Z);
ax.CLim(1) = 0;
ax.Colormap(1, :) = [1 1 1];
c.LevelList(c.LevelList <= 0) = [];
Original contour
contour generated by the given code