3

I'm using pgfplots and plotting the equations r=1 and r=1-sin(\x/2), but I would like to shade the 3 different regions formed by the curves. I found a link to this bug report asking about using fill between in the polaraxis environment which links to 2 other TeX.SX posts: https://github.com/pgf-tikz/pgfplots/issues/124

This post shades the area containing the origin: Shading between the graphs of two polar equations in pgfplots

This post uses the axis environment instead of the polaraxis environment: Shading a region between two polar curves

Here is the code I'm working with:

\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\usepgfplotslibrary{polar, fillbetween}
\pgfplotsset{compat=newest}

\begin{document} \begin{tikzpicture}[scale=1] \begin{polaraxis} [ domain=0:360, samples=180, grid=both, grid style={line width=0.1pt, draw=gray!75}, major grid style={black}, minor x tick num=3, minor y tick num=3, xmin=0, xmax=360, ymin=0, ymax=2.25, xtick={0,45,...,360}, xticklabels={}, ytick={3}, yticklabel style={anchor=north}, ] \addplot[draw=red, domain=0:720] {1-sin(\x/2)}; \addplot[draw=blue, domain=0:360] {1}; \end{polaraxis} \end{tikzpicture} \end{document}

MWE

Stefan Pinnow
  • 29,535

1 Answers1

5

Here is a method that is not very sophisticated but works.

\documentclass{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{polar, fillbetween}
\pgfplotsset{compat=newest}
\makeatletter
\tikzset{reuse path/.code={\pgfsyssoftpath@setcurrentpath{#1}}}
\makeatother

\begin{document} \begin{tikzpicture}[scale=1] \pgfplotsset{set layers} \begin{polaraxis} [axis on top, domain=0:360, samples=180, grid=both, grid style={line width=0.1pt, draw=gray!75}, major grid style={black}, minor x tick num=3, minor y tick num=3, xmin=0, xmax=360, ymin=0, ymax=2.25, xtick={0,45,...,360}, xticklabels={}, ytick={3}, yticklabel style={anchor=north}, ] \addplot[draw=red, domain=0:720,save path=\RedPath] {1-sin(\x/2)}; \addplot[draw=blue, domain=0:360,save path=\BluePath] {1}; \pgfonlayer{axis background} \fill[blue!20,reuse path=\BluePath]; \fill[red!20,even odd rule,reuse path=\RedPath]; \clip[reuse path=\BluePath]; \fill[cyan!20,even odd rule,reuse path=\RedPath]; \endpgfonlayer \end{polaraxis} \end{tikzpicture} \end{document}

enter image description here

  • This is great! I had seen another solution using the reuse path macro, but I think I was trying to use it with scope and that didn't work. – pwesterbaan Nov 29 '20 at 01:57
  • 1
    @pwesterbaan In principle a scope should not be a problem because save path creates global macros, yet this business here is a bit tricky and in the best of all worlds one day the issue you mention gets fixed on the pgfplots side. –  Nov 29 '20 at 02:00