RedBlue Colormap Generator with Zero as White or Black

A colormap generator to display positive values as blue and negative values as red. Zero is displayed as either white or black.
1.2K Downloads
Updated 4 May 2020

View License

Generates a RED-BLUE colormap with zero as white or black. Positive values are displayed as blue intensities and negative values are displayed as red intensities. Zero can be selected to be either white or black. The clim values, from the image or can be entered, are used to customize the colormap.

Syntax: y = redblue(n,clim,'black')
Typical usage: colormap(redblue(64))

Arguments:
All arguments are optional and can be in any order.
n - number of color levels (default is # of colors of current colormap)
clim - two element vector specifying the color limits (default: current axis color limits)
black - string ('k' or 'black') specifying zero as black (default is zero as white)

This creates a custom colormap for any image. The colorbar scale will be skewed toward red or blue depending on the caxis values (clim) of the image.

Two versions:
redblue.m - This version flattens the edges of the colormap to improve the visualization of the
gradient. The effect works better for larger values of n.
redblueu.m - This version uses a uniform scaling for red and blue. The white and black will appear
to be a larger section.

Keep in mind that if the scale is very skewed, there will not be much of a color gradient. The gradient can always be increased by using your own clim values.
% Example:
% y = caxis; % e.g. y = [-11,-5]
% colormap(redblue(64) % and not much gradient
% colormap(redblue(64,[-11,0])) % white is at -5 with a larger gradient

Cite As

Mirko Hrovat (2024). RedBlue Colormap Generator with Zero as White or Black (https://www.mathworks.com/matlabcentral/fileexchange/74791-redblue-colormap-generator-with-zero-as-white-or-black), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2017a
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

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

Start Hunting!
Version Published Release Notes
1.0.1

Colormap generating algorithm now accounts for the case when the differences in the clim limits is very small.

1.0.0