6

I'm trying to shade in just one petal of a polar function. Specifically, I was wondering if there was a way to shade in the bounded region that lives between my two rays:

enter image description here

I've tried searching for examples elsewhere but most of the ones I've seen involve either shading in a region bounded by two curves or shading in the area under a function y=f(x).

\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    smooth,
    trig format plots=rad,
    axis lines=middle,
    axis equal]
\addplot [domain=0:pi, samples=150, red]
         ({2*sin(5*x)*cos(x)}, {2*sin(5*x)*sin(x)});
\draw [-stealth, ultra thick, dashed, draw=blue] 
        (axis cs: 0,0)
        -- ++(canvas polar cs: radius=3cm,angle=0);
\draw [-stealth, ultra thick, dashed, draw=blue] 
        (axis cs: 0,0)
        -- ++(canvas polar cs: radius=4cm,angle=deg{pi/5});
\end{axis}
\end{tikzpicture}%
\end{document}

Any feedback would be greatly appreciated!

  • 2
    Welcome to Tex.SE !! – nidhin Oct 27 at 5:54
6

Since you know the coordinates, just use fill.

\fill[blue,opacity=0.4] (o) -- (a) -- (b);

Note that I have the named the coordinates. See the full code:

\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    smooth,
    trig format plots=rad,
    axis lines=middle,
    axis equal]
\addplot [domain=0:pi, samples=150, red] ({2*sin(5*x)*cos(x)}, {2*sin(5*x)*sin(x)});
\draw [-stealth, ultra thick, dashed, draw=blue] (axis cs: 0,0)coordinate(o)
        -- ++(canvas polar cs: radius=3cm,angle=0)coordinate(a);
\draw [-stealth, ultra thick, dashed, draw=blue] (o)
    -- ++(canvas polar cs: radius=4cm,angle=deg{pi/5})coordinate(b);
\fill[blue,opacity=0.4] (o) -- (a) -- (b);
%  for filling the petal
% \addplot [domain=0:pi/5, samples=30, red,fill=blue!40] ({2*sin(5*x)*cos(x)}, {2*sin(5*x)*sin(x)}); 
\end{axis}
\end{tikzpicture}%
\end{document}

Output:

enter image description here

EDIT

If you want only the petal to be shaded, then you can use the \addplot to fill.

\addplot [domain=0:pi/5, samples=30, red,fill=blue!40] ({2*sin(5*x)*cos(x)}, {2*sin(5*x)*sin(x)});

enter image description here

  • 1
    thanks! I should've been more clear in my question. I only wanted to shade in just the region bounded by the function. Specifically, I just want to shade in the petal. – Donnie Nathan Chad Oct 27 at 13:02
  • 1
    @DonnieNathanChad See the edit. – nidhin Oct 27 at 13:23
4

You could just add a plot that you shade.

\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
    trig format plots=rad,
    axis lines=middle,
    axis equal]
\addplot[stealth-stealth, ultra thick, dashed, draw=blue,fill=blue!30]  
  coordinates {(2,0) (0,0) ({3*cos(pi/5)},{3*sin(pi/5)})};
\addplot [smooth,domain=0:pi, samples=150, red]
         ({2*sin(5*x)*cos(x)}, {2*sin(5*x)*sin(x)});
\end{axis}
\end{tikzpicture}%
\end{document}

enter image description here

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.