0
$\begingroup$

Suppose I want to define a color function to use more than one graph. I'd prefer not to type it out every time. I know that I can set ColorData like this:

Unprotect[ColorData];
ColorData["myRainbow"] = 
  Function[x, Blend[{Black, Purple, Blue, Cyan, Green, Yellow, Orange, Red}, x]];
Protect[ColorData]

But I'm not sure how I can define my own ColorFunction which uses that ColorData. For example, if I try to do something like this:

Unprotect[ColorFunction];
ColorFunction["myCF"] = Function[{y}, ColorData["myRainbow"][y]];
Protect[ColorFunction];

and then try to call that color function with something like this:

nd[y_, a_, b_, n_] := (Binomial[b - a + 1, y]*Sum[((-1)^i)*Binomial[y, i]*((y - i)/(b - a + 1))^n, {i, 0, y}]);
myplot[y_] := 
 ArrayPlot[Table[nd[y, 1, K, n], {K, 1, 50}, {n, 1, 50}], 
  ColorFunction -> "myCF", ColorFunctionScaling -> False, 
  DataReversed -> True, Frame -> True, FrameTicks -> Automatic, 
  FrameLabel -> {Rotate["K", -90 Degree], "N"}]
myplot[5]

I get "Value of option ColorFunction -> mine is not a valid color function, \ or a gradient ColorData entity." So clearly I have done something wrong here, but I honestly cannot figure out what. How do you define your own ColorFunction?

$\endgroup$
  • 7
    $\begingroup$ Why do it like this? Unprotecting and redefining system symbols should be a last resort - it's just a bad idea. In this case you could just define myColorFunction = Function[x, Blend[{Black, Purple, Blue, Cyan, Green, Yellow, Orange, Red}, x]], and then in your plot command, replace ColorFunction -> "myCF" with ColorFunction -> myCF $\endgroup$ – Jason B. Nov 20 '17 at 19:01
  • $\begingroup$ I wasn't aware I could do that. That simplifies things dramatically. I appreciate it! $\endgroup$ – ereHsaWyhsipS Nov 20 '17 at 20:25

Your Answer

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

Browse other questions tagged or ask your own question.