1
$\begingroup$

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]]]

$\endgroup$
1
$\begingroup$
f = 1.0001 x;
Plot[Cos[x], {x, 0, Pi}, ColorFunction -> Function[x, Hue[f]]]

enter image description here

$\endgroup$
  • $\begingroup$ 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. $\endgroup$ – Tom Mar 4 at 10:25
3
$\begingroup$

Use SetDelayed to define f and give it an argument.

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

plot_a

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]

plot_b

$\endgroup$
1
$\begingroup$

Try

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

enter image description here

$\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.