MATLAB: How to customize a colorbar of contourf plot such that different colors are for values in different ranges? The interval in the range may or may not be constant.

colorbarunequal discretization of colorbars

I want to have a contourf plot such that it is giving one particular color for values from 0-1, another from 1-1.25, 1.25-1.75 and 1.75-4. The interval of the range is not constant. Is is possible in MATLAB?. I would appreciate some solution for this problem.

Best Answer

Let z be your data to plot a contour map. And crange be the colors range you want to have.
crange = [ 0 1;1 1.25;1.25 1.75 ; 1.75 4 ; 4 inf] ;
cmap = jet(size(crange,1)) ;
%%Arrange the colors range
colors = zeros(size(z));
for i = 1:size(crange,1)
colors(z > crange(i,1) & z<=crange(i,2)) = crange(i,2);
end
Now use contour/ surf with z. and colormap(cmap); colorbar ;