Here's a demo.
- Produce the contour plot.
- Set the binaryThreshold variable that defines the border of white/black
- Redefine the colormap
Z = peaks;
[~,c] = contourf(Z);
cb = colorbar();
binaryThreshold = 2.0;
ax = gca();
crng = caxis(ax);
clrmap = ax.Colormap;
nColor = size(clrmap,1);
binThreshNorm = (binaryThreshold - crng(1)) / (crng(2) - crng(1));
binThreshRow = round(nColor * binThreshNorm);
newColormap = [ones(binThreshRow,3); zeros(nColor-binThreshRow, 3)];
ax.Colormap = newColormap;
c.LineColor = [.5 .5 .5];
c.LineWidth = 2;
cb.LineWidth = .7;
Note that since all colormaps have a discrete number of colors, the border between black & white is very closely approximated and will most likely not cause a problem. For example, with a colormap of 256 colors, if the threshold is set to 33% of the color range, the transition would happen between color #84 and #85 which would be rounded to 84. If this poses a problem, you can either slightly reduce or increaes the threshold or you can change round() to ceil() or floor().
Best Answer