9
$\begingroup$

Let me pose the question through an example:

Normally, to set the ColorFunction for, say, a ListDensityPlot, I would write

myColorFunc = ColorData[{"GreenPinkTones", {-30, 10}}];
ColorFunctionScaling -> False;

In this example, though, the white of the ColorFunction comes at -10, whilst I would like to have shades of Green from -30 to 0, and shades of Pink from 0 to 10. How can I achieve this?

$\endgroup$
  • $\begingroup$ cf = Blend[{{-30, Green}, {0, White}, {10, Pink}}, #] &; BarLegend[{cf, {-30, 10}}] see Blend $\endgroup$ – Jason B. Feb 8 '17 at 17:39
  • $\begingroup$ You can also look here and here for graphical interfaces to create your own color function $\endgroup$ – Jason B. Feb 8 '17 at 17:45
  • 3
    $\begingroup$ cf = ColorData[{"GreenPinkTones", {-30, 30}}][2 # + Abs@#] & $\endgroup$ – Simon Woods Feb 8 '17 at 21:49
10
$\begingroup$

You could make your own bi-linear mapping:

myColorFunc = (ColorData["GreenPinkTones"][
               Piecewise[{
                          {Rescale[#, {-30, 0 }, {0, 1/2}], # < 0},
                          {Rescale[#, {0  , 10}, {1/2, 1}], # >= 0}}]]) &;

BarLegend[{myColorFunc, {-30, 10}},LegendLayout -> "Row"]

shifted color gradient

| improve this answer | |
$\endgroup$
  • $\begingroup$ Thank you very much. This gets the job done. Thanks also to @JasonB. for his input. Both the solutions work well. This is a great community. $\endgroup$ – Marce Feb 8 '17 at 22:30
3
$\begingroup$

Another way to shift the position of a middle color of a gradient uses the "bias" function of Schlick (with suitable rescaling) along with a color gradient. Here is a small demo:

bias[h_, x_] := bias[h, x, {0, 1}];
bias[h_, x_, {a_, b_}] := a + (x - a)/(1 + (1/h - 2) (1 - (x - a)/(b - a)))

Manipulate[DensityPlot[x, {x, -30, 10}, {y, 0, 5}, AspectRatio -> Automatic, 
                       ColorFunction -> Function[x, ColorData[{gradient, {-30, 10}},
                                                              bias[h, x, {-30, 10}]]], 
                       ColorFunctionScaling -> False, FrameTicks -> {Automatic, None}, 
                       PlotLabel -> Row[{"h=", Round[h, 0.001]}]], 
           Row[{Control[{{gradient, "GreenPinkTones"}, ColorData["Gradients"],
                         ControlType -> PopupMenu}],
                Control[{{h, 0.5}, 0, 1}]}, Spacer[20]]]

example of "biased" gradient

where we see through experimentation that setting h = 0.25 shifts the white color to 0. To confirm:

Solve[bias[h, 0, {-30, 10}] == bias[1/2, Mean[{-30, 10}], {-30, 10}], h]
   {{h -> 1/4}}

Thus, here is the final color function you need:

mycf = ColorData[{"GreenPinkTones", {-30, 10}}, bias[1/4, #, {-30, 10}]] &;
BarLegend[{mycf, {-30, 10}}, LegendLayout -> "Row"]

bar legend with biased color

| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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