[Tex/LaTex] Shading a region between two polar curves

axispgfplotspolarplottikz-pgf

I'm using Tikz and have two polar curves r=1.5+cos(t) and r=1.5. I would like to fill (shade) the area between curves (shown by an arrow on the picture). How can I do this? Maybe using \clip or something?

enter image description here

I tried \tikzfillbetween but it behaves weirdly in polar coordinates.

\documentclass{article}
\usepackage{pgfplots}
\usetikzlibrary{arrows, intersections, fillbetween}
\begin{document}
\begin{tikzpicture}
\begin{axis}[grid = both]
    % Outer curve
    \addplot [data cs=polar, domain=0:360, samples=180, black,
        line width=1pt, smooth](x, {1.5+cos(x)});
    % Inner curve
    \addplot [data cs=polar, domain=0:360, samples=180, green,
        line width=1pt, smooth](x, 1.5);
\end{axis}
\end{tikzpicture} 
\end{document}

Best Answer

You could use intersection segments:

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}% <- added!!
\usepgfplotslibrary{fillbetween}% <- changed
\begin{document}
\begin{tikzpicture}
\begin{axis}[grid = both,
    set layers% <- added
]
    % Outer curve
    \addplot [data cs=polar, domain=0:360, samples=180, black,
        line width=1pt, smooth,
        name path=outer
        ](x, {1.5+cos(x)});
    % Inner curve
    \addplot [data cs=polar, domain=0:360, samples=180, green,
        line width=1pt, smooth,
        name path=inner
        ](x, 1.5);
    % Filling
    \begin{pgfonlayer}{axis background}
      \fill [orange!30,
          intersection segments={
            of=inner and outer,
            sequence={L0--L1--R1[reverse]--R0[reverse]}
          }];
    \end{pgfonlayer}
\end{axis}
\end{tikzpicture} 
\end{document}

Result:

enter image description here


Example showing the segments:

enter image description here

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}% <- added!!
\usepgfplotslibrary{fillbetween}% <- changed
\begin{document}
\begin{tikzpicture}
\begin{axis}[grid = both,
    set layers% <- added
]
    % Outer curve
    \addplot [data cs=polar, domain=0:360, samples=180, black,
        line width=1pt, smooth,
        name path=outer
        ](x, {1.5+cos(x)});
    % Inner curve
    \addplot [data cs=polar, domain=0:360, samples=180, green,
        line width=1pt, smooth,
        name path=inner
        ](x, 1.5);
    % Filling
    \begin{pgfonlayer}{axis background}
        \fill [orange!30,
            intersection segments={
              of=inner and outer,
              sequence={L0--L1--R1[reverse]--R0[reverse]}
            }];
    \end{pgfonlayer}
\end{axis}
% Showing the segments
\begin{scope}[line width=2pt,->,font=\bfseries]
    \foreach[count=\i from 0] \c in {green,orange,purple}{
        \edef\temp{\noexpand\draw [\c,
            intersection segments={of=inner and outer,sequence={L\i}}]node[left]{L\i};}
        \temp}
     \foreach[count=\i from 0] \c in {blue,gray,yellow}{
         \edef\temp{\noexpand\draw [\c,
            intersection segments={of=inner and outer,sequence={R\i}}]node[right]{R\i};}
         \temp}
 \end{scope}
\end{tikzpicture} 
\end{document}
Related Question