MATLAB: Personalising contour plot, map colour scheme

contour plotMATLAB

Hi,
I have 2 sets of data, which when subtracted will give me the absolute performance of a system. Therefore there will be positive and negative values. These values are then plotted on a contour plot to understand which is best. I would like to personalise the colour bar with my own colour scheme as shown below:
-10 to -2: blue, -2 to 2: white, 2 to 10: red.
I would also like the colours to shade with change of magnitude – so the blue starts at dark blue from -10 to light blue to -2, then white between -2 and 2 and light red to dark red from 2 to 10.
I have already managed to plot the contour plot but need to change the colour map.
Matlab code
figure contourf(N_X3,N_Y3,N_Z3,50,'edgecolor','none') %N_X3 is the x axis 1×9 matrix N_Y3 is the y axis 7×1 matrix and N_Z3 is the z axis 7×9 matrix
title(d);
colormap cool %map colour style
xlabel('Peak Period (t)');
ylabel('Heading (deg)');
c = colorbar('southoutside');
c.Label.String = ('MSI%'); %This is how you label the colourbar

Best Answer

Would something like this work for you?:
R = [linspace(0,1,100) ones(1,43) linspace(1,1,100)];
G = [linspace(0,1,100) ones(1,43) linspace(1,0,100)];
B = [linspace(1,1,100) ones(1,43) linspace(1,0,100)];
cmap = [R(:),G(:),B(:)]; %// create colormap

figure
img = peaks(128);
imagesc(img)
daspect([1 1 1])
colormap(cmap)
caxis([-10 10])
colorbar
% You might also want to look at this:
% colormapeditor
Basically every colormap can be described by a Mx3 matrix, so here I have used linspace to make a colormap that ranges from blue [0 0 1] to white [1 1 1] and then to red [1 0 0]. You can read more about color specification here. You can also see this in action if you run some code like this:
jet(256)
This generates a jet colormap with 256 elements and if you run this in the command window you will see that the output is a 256x3 matrix of color specifications.
You could also check out colormapeditor which lets you edit colormaps as you like:
Hope this helps,
M.
EDIT:
For contour the correct code would be this instead:
R = [linspace(0,1,100) ones(1,43) linspace(1,1,100)];
G = [linspace(0,1,100) ones(1,43) linspace(1,0,100)];
B = [linspace(1,1,100) ones(1,43) linspace(1,0,100)];
cmap = [R(:),G(:),B(:)]; %// create colormap
figure
img = peaks(128);
contour(img,128)
daspect([1 1 1])
colormap(cmap)
caxis([-10 10])
colorbar
And the result would look like this:
untitled.png