How can I create a colormap with a different colorscale for positive and negative numbers

How can I create a colormap with a different colorscale for positive and negative numbers: positive numbers on a white to red scale, and negative numbers on a white to blue scale (zero being white).

Answers (3)

Dear Richard,
a colormap is essentially a nx3 matrix with the specification of RGB (3 columns) and the resolution in n lines. You can either create you own "french flag" (I actually did it by myself in some of my scripts),...
% A french-flag with a resolution of 3 would be:
FF = [0 0 1;...
1 1 1;...
1 0 0];
% which becomes:
FF = [0 0 1;...
0.5 0.5 1;...
1 1 1;...
1 0.5 0.5;...
1 0 0];
% by increasing the resolution.
..or download some very nice colormaps like these ones . The one you are looking for is RdBu.
Follow the instruction on how to use the downloaded file and then retreive the colormap as following:
MyMap = brewermap(100, 'RdBu'); %with 100 being the number of steps in your colormap
myMap = flipud(MyMap); %Since you want blue for negative and red for positive values
Hope this helps. Best

10 Comments

Thank you for the link to my FEX submission brewermap. Note that flipud is not required because I wrote brewermap to reverse the colormap sequence when the string token is prefixed with '*', like this:
map = brewermap(100, '*RdBu');
Some other bleu-blanc-rouge colormap submissions that might be of interest:
Thank you for the answers. The problem is a that the scale is not symmetrical e.g. from -0.134 (blue) to +1.000 (red) and I want zero to be white !
Thank you for specifying this, I must have missed this indication! And thank you also for sharing these colormaps, which I use now by default (I really love the "spectral").
Best,
Richard, you should manually change the axis (which function are you using to plot?) such that they lay symmetrically around zero.
If you aren't worried about the numbers on the colourbar you could just scale your negative data up to a maximum of -1 instead of -0.134 by multiplying by 1/0.134
I am using contourf. The data are inherently non-symmetric about zero, I can't do anything about that!
This should do the trick:
set(gca, 'CLim', [-1*max(abs(min), max), max(abs(min), max)]);
change min e max with actual min and max of the data you are plotting
This doesn't work because the scale becomes -1 (blue) to +1 (red) and the negative data don't show up as they are small with respect to -1. I would like the data scale to go from min ( ~-0.13) to max (1.0) with zero=white, positive numbers between white and red, negative numbers between white and blue. Is this possible ?
@Richard Walker: you could:
  • create your own colormap which has the white offcenter, or
  • specify the colors explicitly when the data is plotted.
There are infinite possible colormaps so it is clearly not feasible for MATLAB to support them all, but it is easy to define your own to suit your data distribution.
Thanks Stephen, "specify the colors explicitly when the data is plotted." sounds like a good solution, but even after searching I can't see how contourf or any other command will do it. I would be grateful for a tip on how to do it.

Sign in to comment.

Thank you Walter. I am sure it must be possible using cptcmap, it looks very powerful, but therefore also rather complex. I got the example in the link you sent to work, but I can't immediately see how to change the scales to what I want .. I am sure there must be a simpler way using basic MATLAB commands, but as a non-expert it's not that simple to penetrate the documentation.

Categories

Asked:

on 19 Jun 2018

Answered:

on 20 Jun 2018

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!