Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Since PolarPlot doesn't support Filling, what is the best way to shade or fill the a region between two polar curves?

For instance, how would I generate a version of the following graph with the region inside the first curve but outside the second curve filled?

PolarPlot[{{1, -1} Sqrt[2 Cos[t]], 2 (1 - Cos[t])}, {t, -\[Pi], \[Pi]}]

Mathematica graphics

share|improve this question
1  
In the first one there are two curves ... :) –  belisarius May 4 '12 at 1:18
    
@belisarius: :) okay, true, but I was actually dealing with r^2==2Cos[t] as my first curve and for purposes of the region to be shaded, we could actually toss out the negative square root part. –  Isaac May 4 '12 at 1:21
    
It appears I misunderstood the question. Should I leave my answer or delete it? Perhaps it illustrates something of value. –  Mr.Wizard May 4 '12 at 5:31
    
@Mr.Wizard: It definitely adds value—if you take out the Reverse@, the part left in white is the part I'd wanted to shade, so with only a little more tweaking, I can get it to do what I'd wanted. –  Isaac May 4 '12 at 6:07
    
Thanks. Though not robust at least that is on-topic; I'll add it to the answer. –  Mr.Wizard May 4 '12 at 6:43

8 Answers 8

up vote 21 down vote accepted

You have a (or more) curves. If you don't use PolarPlot you could use ParametricPlot instead but you would have to make the transformation from polar coordinates by yourself.

Knowing this, you could think about what your functions mean. For instance 2 (1 - Cos[phi]) is just the radius of your curve for a given phi. If you want to draw the region outside your curve, the only thing you have to do is (attention, I'm mixing polar and Cartesian coord.):

Check a every point $\{x,y\}$ whether the radius $\sqrt{x^2+y^2}$ is larger than $2(1-\cos(\varphi))$ where $\varphi=\arctan(y/x)$.

Using this, your filling can be achieved with RegionPlot and your graphics

Show[
 PolarPlot[{{1, -1} Sqrt[2 Cos[t]], 
   2 (1 - Cos[t])}, {t, -\[Pi], \[Pi]}],
 RegionPlot[
  Sqrt[x^2 + y^2] > 2 (1 - Cos[ArcTan[x, y]]) &&
  Sqrt[x^2 + y^2] < Re@Sqrt[2 Cos[ArcTan[x, y]]]
  , {x, -2, 2}, {y, -3, 3}],
 PlotRange -> All
 ]

enter image description here

share|improve this answer

Just another way:

<< VectorAnalysis`;
{rho, t, z} = CoordinatesFromCartesian[{x, y, z}, Cylindrical]
Quiet@Show[
  PolarPlot[{ Sqrt[2 Cos@t], 2 (1 - Cos@t)}, {t, -Pi, Pi}],
  RegionPlot[ Sqrt[2 Cos@t] > rho > 2 (1 - Cos@t), {x, 0, 2}, {y, -1, 1}]]

Mathematica graphics

share|improve this answer

Yet another way. It's similar to belisarius's solutions but doesn't require an inverse coordinate transformation

Show[
 PolarPlot[{Sqrt[2 Abs[Cos[t]]], 2 (1 - Cos[t])}, {t, -\[Pi], \[Pi]}], 
 RegionPlot[4 (1 - Cos[t])^2 < r^2 < 2 Cos[t], {r, 0, 3}, {t, -Pi, Pi},
  PlotPoints -> 30] /. 
   GraphicsComplex[a_, b__] :> GraphicsComplex[#1 {Cos[#2], Sin[#2]} & @@@ a, b]]

Mathematica graphics

share|improve this answer

You can parameterize your polar functions on to discs, and then shade appropriately.

ρ[t_] := Sqrt[2 Cos[t]];
σ[t_] := 2 (1 - Cos[t]);
ParametricPlot[{{r Cos[t] ρ[t], r Sin[t] ρ[t]}, {r Cos[t] σ[t], r Sin[t] σ[t]}}, 
    {t, -π, π}, {r, 0, 1}, PlotStyle -> {{Opacity[.5], Red}, {Opacity[1], White}}, 
    Mesh -> None, PlotRange -> All]

enter image description here

share|improve this answer

Combining ListLinePlot, Filling and Overlay:

   pnts = Cases[
      PolarPlot[{{1, -1} Sqrt[2 Cos[t]], 2 (1 - Cos[t])}, {t, -\[Pi], \[Pi]}],
     _Line, {0, Infinity}];
   Table[ListLinePlot[{Join[pnts[[1, 1]], pnts[[2, 1]]], pnts[[3, 1]]}, 
     Filling -> fllng, PlotStyle -> {Red, Green},  AspectRatio -> 1], 
   {fllng, {Automatic, {1 -> {Axis, White}}, {2 -> {Axis, White}}, {1 -> {Axis, Red}},
    {2 -> {Axis, Green}}}}]

gives

enter image description here

With Overlay and

  GraphicsRow[{Overlay[%[[{4, 3}]]], Overlay[%[[{5, 2}]]]}]

we get

enter image description here

share|improve this answer

Here's a meager attempt that will soon be humiliated by Heike's answer. ;-)

g = PolarPlot[{{1, -1} Sqrt[2 Cos[t]], 
    2 (1 - Cos[t])}, {t, -\[Pi], \[Pi]}];

Graphics[{
   {Pink, Rectangle[Scaled[{0, 0}], Scaled[{1, 1}]]},
   Thread@{{White, Green, Blue}, 
     Reverse@Cases[g, Line[x__] :> Polygon[x], ∞]}
   }];

Show[g, %, g]

Mathematica graphics


Seeing as I misunderstood the question, here is an admittedly fragile way to shade the correct region:

g = PolarPlot[{{1, -1} Sqrt[2 Cos[t]], 2 (1 - Cos[t])}, {t, -\[Pi], \[Pi]}];

f = Graphics[Thread@{{Yellow, White, White}, Cases[g, Line[x__] :> Polygon[x], ∞]}];

Show[g, f, g]

Mathematica graphics

share|improve this answer

Even if Filling were an option in PolarPlot, you won't be able to create such a plot because for 2D graphics, Filling just blindly fills along the y-axis, whereas you need to check for an inequality here.

That said, here's another approach that's in the same spirit as halirutan's, but you don't have to convert to Cartesian, etc.

eqns[t_] := { Sqrt[2 Cos[t]], 2 (1 - Cos[t])};
region = PolarPlot[eqns[t], {t, -π, π}, 
    RegionFunction -> Function[{x, y, t, r}, {#1 > #2} & @@ Re[eqns[t]] // First]];
pts = Cases[region, Line[x___] :> x, Infinity];
colors = {Darker@Green, Blue};

Show[
    PolarPlot[Evaluate@eqns[t], {t, -π, π}, PlotStyle -> colors], 
    ListLinePlot[pts, PlotStyle -> colors, Filling -> Axis, FillingStyle -> LightGreen], 
    PlotRange -> All
]

enter image description here

share|improve this answer

How about

dt = Pi/99;
pts = Join[
   Table[2 (1 - Cos[t]) {Cos[t], Sin[t]},
    {t, 0, -Pi/3 + dt, -dt}],
   Table[Sqrt[2 Cos[t]] {Cos[t], Sin[t]},
    {t, -Pi/3, Pi/3, dt}],
   Table[2 (1 - Cos[t]) {Cos[t], Sin[t]},
    {t, Pi/3, dt, -dt}]
   ];
PolarPlot[{Sqrt[2 Cos[t]], 2 (1 - Cos[t])}, {t, -\[Pi], \[Pi]},
 Prolog -> {LightGray, Polygon[pts]},
 PlotStyle -> Thick]

enter image description here

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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