Control colorbar scale in MATLAB

I think all that you’re missing is a call to caxis to specify the minimum and maximum values to map the color range to:

caxis([18 23]);

enter image description here

Note that the following line…

cb.Limits = [18 23];

… only changes the tick limits displayed on the colorbar, but doesn’t change anything about how the data is mapped to the color range. The caxis function is how you control that (in the above case, mapping the value of 18 to one end and the value of 23 to the other). By default, your code was mapping the minimum and maximum values in Z to the color range (20.5 and 23, respectively). When you then set the tick limits on the color bar to a larger range, it just filled it in with the last color in the color map, in this case red. That’s why you see so much of it.

Bonus

Just because you might be interested, you could also use interpolation via the interp1 function to easily generate your color map like so:

cmap = interp1([1 0 0; 1 1 0; 0 1 0], linspace(1, 3, 41));

Leave a Comment