7
$\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$
  • $\begingroup$ If you're just plotting $y=f(x)$, it's simpler to use Plot[] with its Filling option... $\endgroup$ – J. M. will be back soon Aug 28 '12 at 5:25
7
$\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$
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$
  • 1
    $\begingroup$ As always, credit to Heike for her torn[] magic, and to Szabolcs for his super-hyper image uploader palette $\endgroup$ – Dr. belisarius Aug 28 '12 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 Nov 24 '14 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$
  • $\begingroup$ Sadly I don't have enough reputation to post an image. $\endgroup$ – jens_bo Aug 28 '12 at 6:06
  • 1
    $\begingroup$ I did it for you ... now you have some rep $\endgroup$ – Dr. belisarius Aug 28 '12 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$ – Dr. belisarius Aug 28 '12 at 6:11
  • $\begingroup$ Thanks a lot. I will try to collect some more points now :) $\endgroup$ – jens_bo Aug 28 '12 at 8:08

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.