MATLAB: Colormap based on contour values

caxiscolor rampcolor scalecolormapmappingmapping properties

I want to make my color map such that it uses white for the 0 value and applies a diverging ramp of color (blue and red) for cmax and cmin. The example map I have is temperature anomalies. I can go through and manually adjust the RGB values so that white is 0, but I was hoping there was an easier way to automate the process so that the 0 value in the axis range is automatically white and the values diverge from there (either toward darker blue for negative or toward darker red for positive values). Any help would be great!

Best Answer

function cmap = geblueredcolormap(cmin, cmax)
assert(cmin<0 & cmax> 0, 'colormap does not cross zero');
zeroidx = round(-cmin*255/(cmax-cmin));
bluesat = linspace(1, 0, zeroidx);
redsat = linspace(0, 1, 256-zeroidx);
hsv = [repmat([0 1 1], zeroidx, 1); repmat([2/3 1 1], 255-zeroidx, 1)];
hsv(:, 2) = [bluesat, redsat(2:end)];
cmap = hsv2rgb(hsv);
end
Will allow you to create the correct colormap