8
$\begingroup$

I am using ParametricPlot to get an X vs Y plot. I want to shade the area enclosed by the curve falling in the region where Y<0.

For example, how to shade the area enclosed by the Sin curve where Sin[x]<0?

ParametricPlot[{x, Sin[x]}, {x, 0, 10}]

enter image description here

$\endgroup$
1
  • $\begingroup$ If you're just plotting $y=f(x)$, it's simpler to use Plot[] with its Filling option... $\endgroup$ Commented Aug 28, 2012 at 5:25

3 Answers 3

8
$\begingroup$
 ParametricPlot[{ u, If[Sin[u] < 0, v, 1] Sin[u]},
   {u, -2 Pi, 2 Pi}, {v, 0, 1},
   AspectRatio -> 1, Mesh -> False,
   PlotStyle -> Directive[Red, Opacity[.8]]]

enter image description here

ParametricPlot[{ u, If[Sin[u] < 0, v, 1] Sin[u]},
 {u, -2 Pi, 2 Pi}, {v, 0, 1},
 Mesh -> False, AspectRatio -> 1, 
 ColorFunction -> Function[{x, y, u, v}, Hue[v]], 
 PlotPoints -> 100]

enter image description here

Update: Few alternatives that use {u, v Sin[u]} as the first argument of ParametricPlot:

Using ColorFunction:

ParametricPlot[{ u, v Sin[u]}, {u, -2 Pi, 2 Pi}, {v, 0, 1},
AspectRatio -> 1, Mesh -> False,
ColorFunction -> (If[Sin[#1] > 0, White, Red] &),
ColorFunctionScaling -> False,
PlotPoints -> 100]

Using Mesh, MeshFunctions and MeshStyle:

ParametricPlot[{u, v Sin[u]}, {u, -2 Pi, 2 Pi}, {v, 0, 1}, 
   AspectRatio -> 1,
   Mesh -> {{0.}},
   MeshFunctions -> {Function[{x, y, u, v}, y]},
   MeshShading -> {Red, White},
   PlotPoints -> 100]
$\endgroup$
1
  • $\begingroup$ All these solutions are extremely slow in case if you have a time-consuming function instead of Sin[u]. Timing is much smaller if the second variable v is dropped and no filling is made. $\endgroup$ Commented Jan 31, 2023 at 4:54
6
$\begingroup$

You could do:

ParametricPlot[{{x, Sin[x]}, {x, y}}, {x, 0, 2 Pi}, {y, 0, Min[0, Sin@x]}, 
               Mesh -> None, PlotPoints -> 50, PlotRange -> {{0, 2 Pi}}]

Mathematica graphics

Edit

Using Plot[] is much easier

Plot[Sin@x, {x, 0, 2 Pi}, Filling -> {1 -> {Axis, {Yellow, None}}}]

Mathematica graphics

$\endgroup$
2
  • 1
    $\begingroup$ As always, credit to Heike for her torn[] magic, and to Szabolcs for his super-hyper image uploader palette $\endgroup$ Commented Aug 28, 2012 at 6:18
  • $\begingroup$ torn[] - tearing apart all context and courtesy by unasked for boldness, since no idea when! Thank you for the visual harassment I guess ^^ $\endgroup$
    – Cetin Sert
    Commented Nov 24, 2014 at 2:33
5
$\begingroup$

Evaluation is faster than Verde's solution plus this little deviation close to the intersection is gone: Make a second plot of y Min[Sin[x],0.0] with y as an additional variable, so you can use the mesh to fill the area.

ParametricPlot[{{x, Sin[x]}, {x,y Min[Sin[x],0.0]}},
                 {x, 0, 2 Pi }, {y, 0, 1}, PlotRange -> All, AspectRatio -> 1, Mesh -> False]

Mathematica graphics

But it does not work perfectly, e.g. for a filling sin(x)<0.1.

$\endgroup$
4
  • $\begingroup$ Sadly I don't have enough reputation to post an image. $\endgroup$
    – jens_bo
    Commented Aug 28, 2012 at 6:06
  • 1
    $\begingroup$ I did it for you ... now you have some rep $\endgroup$ Commented Aug 28, 2012 at 6:08
  • 1
    $\begingroup$ Hi! welcome to Mathematica.SE! Please consider registering your account so that any upvotes you get on this answer are added to those you might get on future questions and answers. That way, over time you will be able to do more on the site (post graphics, edit things, etc). $\endgroup$ Commented Aug 28, 2012 at 6:11
  • $\begingroup$ Thanks a lot. I will try to collect some more points now :) $\endgroup$
    – jens_bo
    Commented Aug 28, 2012 at 8:08

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.