For MATLAB versions R2014b and later:
As of MATLAB R2014b, you can accomplish this with the 'colormap' function. You can use the 'colormap' function to customize the range of colors that gets displayed. You can create a completely custom colormap by specifying each RGB value:
map = [0, 0, 0.3;
0, 0, 0.4;
0, 0, 0.5;
0, 0, 0.6;
0, 0, 0.8;
0, 0, 1.0];
colormap(map)
Or if you would like a starting point for creating the values in 'map', you can use values from one of the built-in colormaps:
For MATLAB versions R2014a and earlier:
The ability to set the number of colors in a colorbar is not directly available in MATLAB.
However, this is possible by changing the 'CData' property of the image that resides in the COLORBAR axes, as shown in the example below:
mx = peaks(100);
numberOfColors = 5;
nc = numberOfColors-1;
[c,h, cf] =contourf(mx,nc);
cb = colorbar;
i = findobj(cb,'type','image');
minVal = min(mx(:));
maxVal = max(mx(:));
set(i,'cdata',[0:64/nc:64]','YData',[minVal maxVal]);
Finally, set the tick marks of the colorbar:
set(cb,'yLim',[minVal maxVal],'ytick',[minVal:(maxVal-minVal)/(nc):maxVal]');
You can refer to the documentation for more details and examples:
Best Answer