Is there a way to control the scaling of ColorFunction beside just to turn it off and on with ColorFunctionScaling?

Two problems with this approach are:

  1. Repeating colours if the scaling is small compared to the plot range.

    enter image description here

  2. Uniform colour if the scaling is large compared to the plot range.

    enter image description here


1 Answer 1


By default, ColorFunctionScaling -> True rescales the value to lie between 0 and 1. You can explicitly control the scaling of the ColorFunction by setting ColorFunctionScaling -> False and implementing the rescaling in the ColorFunction itself. The following examples should clarify what these options do and how to design your own:

Default behaviour

Plot[2 Cos[x], {x, 0, 4 π}, PlotStyle -> AbsoluteThickness[3], ImageSize -> 300,
    ColorFunction -> ColorData["DarkRainbow"], ColorFunctionScaling -> #] & /@ {True, False} // Row

You see that with True, all the values are rescaled to lie between 0 and 1 before passing to the ColorFunction, whereas with False, the values greater than 1 and less than 0 are clipped to 1 and 0 respectively.

In other words, the following setting (for the example above):

ColorFunctionScaling -> True, ColorFunction -> cf

is equivalent to

ColorFunctionScaling -> False, ColorFunction -> (cf@Rescale[#2, {min, max}, {0, 1}]&)

where cf is the color function and min and max are the minimum and maximum values of the function you're plotting (for the values being plotted), which in this case are -2 and 2 respectively.

Custom rescaling

The explanation above should give you an idea of how to go about implementing your custom rescaling function. Here's an example that exponentially varies from approximately 0.22 to 1:

Plot[2 Cos[x], {x, 0, 4 π}, PlotStyle -> AbsoluteThickness[3], ColorFunctionScaling -> False,
    ColorFunction -> (ColorData["DarkRainbow"]@Exp@Rescale[#2, {-2, 2}, {-1.5, 0}]&)]


This site is temporarily in read-only mode and not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .