MATLAB: Create a custom colormap

colormapvisualization

hi all,
I would like to customize a colormap with specified color for each value range (from -4 to 2, a total of 7 intervals, and assign a color for each interval):
% a three-column matrix of RGB triplets for each interval:
map = [0.4940 0.1840 0.5560
0.8500 0.3250 0.0980
0.9290 0.6940 0.1250
1 1 0
0.4660 0.6740 0.1880
0 0.75 0.75
0 0.5 0];
% here is part of my codes:
image = imshow(x);
……
caxis([-4 2])
colorbar('Ticks',[-4 -3 -2 -1 0 1 2],…
'TickLabels',{'-4','-3','-2','-1','0','1','2'})
colormap(map)
The location of the label tick is not on the boundary between two color and I don't know how to fix it. Does anyone have an idea on this question?

Best Answer

Because there are 6 unit-sized intervals between -4 and 2, you should use 6 colors in the map. Try this
map = [0.4940 0.1840 0.5560
0.8500 0.3250 0.0980
0.9290 0.6940 0.1250
1 1 0
0.4660 0.6740 0.1880
0 0.75 0.75];