1
$\begingroup$

I want to ask Mathematica to show the domain of negative parts of the function $\sin x$ as red lines on the $x$ axis, like this picture

enter image description here

Is it possible to do it automatically? If not, I will be grateful if please tell me how I can do it manually.

Plot[Sin[x], {x, 0, 20}]

$\endgroup$
1
  • 1
    $\begingroup$ @user64494 - your approach can be used with Plot[{Max[Sin[x], 0], Sin[x]}, {x, 0, 20}, PlotStyle -> {Red, ColorData[97][1]}] $\endgroup$
    – Bob Hanlon
    Nov 3, 2020 at 15:05

2 Answers 2

5
$\begingroup$

1. Use Plot with two functions ({Sin[x], If[Sin[x] <= 0, 0]}) in the first argument:

Plot[{Sin[x], If[Sin[x] <= 0, 0]}, {x, 0, 20}, 
 PlotStyle -> {Automatic, Directive[Red, Opacity[1], Thick]},
 Method -> {"AxesInFront" -> False}]

enter image description here

2. Use NumberLinePlot + Plot

Show[Plot[Sin[x], {x, 0, 20}], 
 NumberLinePlot[Sin[x] < 0, {x, 0, 20}, 
   PlotStyle -> Directive[Thick, Red], Spacings -> 0,
   Method -> {"AxesInFront" -> False}]]

enter image description here

$\endgroup$
2
$\begingroup$

Also Mesh, MeshShading and MeshFunctions

a = Plot[0, {x, 0, 20}, Mesh -> {{0}}, MeshFunctions -> (Sin[#] &), 
   MeshShading -> {Directive[Thickness[.01], Red], None}, 
   MeshStyle -> Directive[PointSize[Large], Green]];
b = Plot[Sin[x], {x, 0, 20}];
Show[b, a]

enter image description here

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