FAQ
overflow

Great Answers to
Questions About Everything

QUESTION

Sin[x]

Show[Plot[Sin[x], {x, 0, \[Pi]}, PlotStyle -> Red, 
          PlotRange -> {{0, 2 \[Pi]}, {-1, 1}}], 
     Plot[Sin[x], {x, \[Pi], 2 \[Pi]}, PlotStyle -> Green, 
          PlotRange -> {{0, 2 \[Pi]}, {-1, 1}}]]

Is there a simpler way of doing this? Especially, one that does not require repeating the Plot command 2 times ;)

{ asked by Andrei }

ANSWER

 Plot[Sin[x], {x, 0, 2 \[Pi]}, PlotStyle -> Thick,
 ColorFunction -> Function[{x, y}, If[x < Pi, Red, Blue]],
 ColorFunctionScaling -> False,
 PlotRange -> {{0, 2 \[Pi]}, {-1, 1}}]

or

 Plot[Sin[x], {x, 0, 2 \[Pi]}, PlotStyle -> Thick,
 Mesh -> {{Pi}},
 MeshShading -> {Red, Blue},
 PlotRange -> {{0, 2 \[Pi]}, {-1, 1}}]

or

 Plot[{ConditionalExpression[Sin[x], 0 <= x <= Pi],
 ConditionalExpression[Sin[x], Pi <= x <= 2 Pi]}, {x, 0, 2 Pi},
 PlotStyle -> {Directive[Thick, Red], Directive[Thick, Blue]}]

enter image description here

{ answered by kguler }
Tweet