# Using previously defined function in ColorFunction

I want to use a previously defined function as part of a function definition for ColorFunction. I tried the following and it worked:

f = x;
Plot[Cos[x], {x, 0, Pi}, ColorFunction -> Function[Evaluate@f, Hue[x]]]


However if I slightly tweak the function it stops working. So my guess is that my approach is wrong from the beginning. How should I be approaching this?

f = 1.0001 x;
Plot[Cos[x], {x, 0, Pi}, ColorFunction -> Function[Evaluate@f, Hue[x]]]


f = 1.0001 x;
Plot[Cos[x], {x, 0, Pi}, ColorFunction -> Function[x, Hue[f]]]


• Ah yes, I think I just made a mistake. I should specify the variable first and then give the function inside Hue. This is what I was looking for.
– Tom
Commented Mar 4, 2019 at 10:25

Use SetDelayed to define f and give it an argument.

f[x_] := Hue[x]
Plot[Cos[x], {x, 0, Pi}, ColorFunction -> f]


Then, should you tweak f you don't have make any change to the Plot expression.

f[x_] := Hue[Cos[x]]
Plot[Cos[x], {x, 0, Pi}, ColorFunction -> f]


Try

f[x_] := Hue[x]
Plot[Cos[x], {x, 0, Pi}, ColorFunction -> (Function[{x}, f[x] ] )]