Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

share|improve this question
Possible duplicate of Use ColorFunction in ListLinePlot with If – MarcoB 2 days ago
Possible duplicate of Set the scale in ColorFunction – Karsten 7. 4 hours ago
up vote 12 down vote accepted

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}]&)]

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.