This example shows how to control the mapping of data values into the colormap so that positive data values map to different colors and negative data values map to black.
Create a surface plot of the peaks function and add a colorbar.
figure surf(peaks(30)) colorbar;
Use caxis to return cmin and cmax, the current data values that map to the minimum and maximum values of the colormap, respectively. Then, use caxis again to change the scaling of data values into the colormap. Set the minimum color limit to zero to map negative data values to the first color in the colormap. Keep the same maximum color limit, cmax.
[cmin,cmax] = caxis; caxis([0,cmax])
Set the first color in the colormap to black by setting the first row of the current colormap, map, to [0,0,0]. Apply the updated colormap to the figure.
map = colormap; % current colormap map(1,:) = [0,0,0]; colormap(map)
All data values less than or equal to zero map to black.