Wolfram Research

Function Repository Resource:

DivergentColorFunction

Source Notebook

Create a diverging color map with a neutral central color for scientific visualization

Contributed by: Jason Biggs

ResourceFunction["DivergentColorFunction"][col1, col2]

returns a diverging color map that interpolates between col1 and col2, with a neutral, unsaturated color in the center of the input range.

ResourceFunction["DivergentColorFunction"]["CoolToWarm"]

returns the color map demonstrated in the paper “Diverging Color Maps for Scientific Visualization” by Kenneth Moreland.

ResourceFunction["DivergentColorFunction"]["scheme"]

returns a diverging color map interpolating between ColorData["scheme"][0] and ColorData["scheme"][1]

Details and Options

ResourceFunction["DivergentColorFunction"] will return a pure function of the form Blend[colors,#]&, which takes values between 0 and 1 and returns an RGBColor.
col1 and col2 can be any ColorQ object, or a list of three real numbers taken to be RGB values.
Diverging color maps are also known as ratio, bipolar or double-ended color maps.
These color functions are useful for visualizing data that is symmetric around some special value, e.g. 0.

Examples

Basic Examples

Choose two colors and create a color function from them:

In[1]:=
colors = {Darker[XYZColor[1, 0.2, 1]], LUVColor[.16, .5, 1]}
Out[1]=
In[2]:=
cf = ResourceFunction["DivergentColorFunction"][colors]
Out[2]=
In[3]:=
DensityPlot[Cos[x] Sin[y], {x, -2 \[Pi], 2 \[Pi]}, {y, -\[Pi], \[Pi]},
  ColorFunction -> cf, PlotLegends -> Automatic, PlotPoints -> 50]
Out[3]=

Create a color function using a built-in color scheme:

In[4]:=
DensityPlot[Cos[x] Sin[y], {x, -2 \[Pi], 2 \[Pi]}, {y, -\[Pi], \[Pi]},
  ColorFunction -> ResourceFunction["DivergentColorFunction"]["RoseColors"], PlotLegends -> Automatic, PlotPoints -> 50]
Out[4]=

Compare this with the same plot using the unmodified scheme:

In[5]:=
DensityPlot[Cos[x] Sin[y], {x, -2 \[Pi], 2 \[Pi]}, {y, -\[Pi], \[Pi]},
  ColorFunction -> "RoseColors", PlotLegends -> Automatic, PlotPoints -> 50]
Out[5]=

Neat Examples

Create a plotting function to visualize color maps:

In[6]:=
showcolorfunction[color_] := With[{opts = {PlotRange -> All, ColorFunction -> color, PlotPoints -> 40, PlotRangePadding -> None, ImageSize -> 200}}, Column[{DensityPlot[
      Cos[x] Sin[y], {x, -2 \[Pi], 2 \[Pi]}, {y, -\[Pi], \[Pi]}, FrameTicks -> None, AspectRatio -> 1/4, opts], DensityPlot[
      10 Cos[x^2] Exp[y], {x, -2 \[Pi], 2 \[Pi]}, {y, -\[Pi], 0}, FrameTicks -> None, AspectRatio -> 1/2, opts], DensityPlot[x, {x, -1, 1}, {y, 0, 1}, FrameTicks -> {{None, None}, {Automatic, None}}, AspectRatio -> 1/10, opts]}, Center, 0]];

Now recreate the example color functions from Kenneth Moreland’s “Diverging Color Maps for Scientific Visualization”:

In[7]:=
showcolorfunction /@ ResourceFunction[
  "DivergentColorFunction"] @@@ {{{0.23, 0.299, 0.754}, {0.706, 0.016,
      0.150}}, {{0.436, 0.308, 0.631}, {0.759, 0.334, 0.046}}, {{0.085, 0.532, 0.201}, {0.436, 0.308, 0.631}}, {{0.217,
      0.525, 0.910}, {0.677, 0.492, 0.093}}, {{0.085, 0.532, 0.201}, {0.758, 0.214, 0.233}}}
Out[7]=

Requirements

Wolfram Language 11.3 (March 2018) or above

Resource History

Source Metadata

License Information