1
$\begingroup$

I am attempting to generate a plot for which the color changes for selected values of x as determined by a function. Below, is my best, unsuccessful attempt. The specific problem with this is that it worked only once for x = 3Pi/4 but not for x = Pi/4. I am stumped, please help!

Plot[
 {Cos[x], Sin[x]}, {x, 0, Pi},
 ColorFunction -> Function[
   x,
   If[Abs[Cos[x]] > Abs[Sin[x]], Red, Blue]
   ]
 ]

enter image description here

$\endgroup$
2
  • 1
    $\begingroup$ Plot[{Cos[x], Sin[x]}, {x, 0, Pi}, ColorFunction -> Function[x, If[Abs[Cos[x]] > Abs[Sin[x]], Red, Blue]], ColorFunctionScaling -> False] $\endgroup$
    – cvgmt
    Sep 27, 2020 at 7:44
  • 1
    $\begingroup$ Plot[{Sin[x], Cos[x]}, {x, 0, Pi}, MeshFunctions -> Function[{x, y}, Abs[Cos[x]] - Abs[Sin[x]]], MeshShading -> {Blue, Red}, Mesh -> {{0}}] is another way. $\endgroup$
    – cvgmt
    Sep 27, 2020 at 8:19

1 Answer 1

1
$\begingroup$

You need to set ColorFunctionScaling to False, otherwise the x coordinate will be rescaled to lie between 0 and 1 before it is passed to the color function.

Plot[
 {Cos[x], Sin[x]}, {x, 0, Pi},
 ColorFunction -> Function[
   {x, y},
   If[
    Abs[Cos[x]] > Abs[Sin[x]],
    Red,
    Blue
    ]
   ],
 ColorFunctionScaling -> False
 ]

Mathematica graphics

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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