7

I tried to use the fillbetween library of pgfplots to fill the intersection of two circles. However I get it with "inverted" colors:

\documentclass{standalone}

\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}

\begin{document}

  \begin{tikzpicture}
    \begin{axis}[axis equal,no markers,hide axis,samples=100]
      \addplot+[domain=360:0,name path=k1] ({1.5*cos(x)},{1.5*sin(x)});
      \addplot+[domain=360:0,name path=k2] ({cos(x) + 2},{sin(x)});
      \addplot[blue!50] fill between[of=k2 and k1];
    \end{axis}
  \end{tikzpicture}


\end{document}

Output:

Output

The white area should be blue and the blue area white. Why does pgfplots fill it this way? How can I fix it?

8

You can use intersection segments:

\documentclass[margin=5pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}
\usepgfplotslibrary{fillbetween}
\begin{document}
  \begin{tikzpicture}
%
    \pgfdeclarelayer{pre main}
    \pgfsetlayers{pre main,main}
%
    \begin{axis}[axis equal,no markers,hide axis,samples=100]
      \addplot+[domain=360:0,name path=k1] ({1.5*cos(x)},{1.5*sin(x)});
      \addplot+[domain=360:0,name path=k2] ({cos(x) + 2},{sin(x)});
%
      \begin{pgfonlayer}{pre main}
        \fill [orange!50,
          intersection segments={
            of=k1 and k2,
            sequence={L0--L1--R2}
          }];
      \end{pgfonlayer}
%
    \end{axis} 
  \end{tikzpicture}
\end{document}

enter image description here

To show the intersection segments:

enter image description here

So the green (L0), orange (L1) and yellow (R2) segments are needed in the sequence to fill the intersection of the two cirles.

Code:

\documentclass[margin=5pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}
\usepgfplotslibrary{fillbetween}
\begin{document}
  \begin{tikzpicture}
    \pgfdeclarelayer{pre main}
  \pgfsetlayers{pre main,main}
    \begin{axis}[axis equal,no markers,hide axis,samples=100]
      \addplot+[domain=360:0,name path=k1] ({1.5*cos(x)},{1.5*sin(x)});
      \addplot+[domain=360:0,name path=k2] ({cos(x) + 2},{sin(x)});
%
      \begin{scope}[line width=2pt]
        \foreach[count=\i from 0] \c in {green,orange,purple}{
          \edef\temp{\noexpand\draw [\c,
            intersection segments={of=k1 and k2,sequence={L\i}}]node[right]{L\i};}
          \temp}
        \foreach[count=\i from 0] \c in {blue,gray,yellow}{
          \edef\temp{\noexpand\draw [\c,
            intersection segments={of=k1 and k2,sequence={R\i}}]node[left]{R\i};}
          \temp}
      \end{scope}
%
  \end{axis} 
  \end{tikzpicture}
\end{document}
2

Thanks to the link posted by @fpast the following code works:

\documentclass{standalone}

\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}

\begin{document}

  \begin{tikzpicture}
    \begin{axis}[axis equal,no markers,hide axis,samples=100]
      \addplot+[domain=360:0,name path=k1,fill=blue] ({1.5*cos(x)},{1.5*sin(x)});
      \addplot+[domain=360:0,name path=k2,fill=blue] ({cos(x) + 2},{sin(x)});
      \tikzfillbetween[of=k1 and k2]{white};     
    \end{axis}
  \end{tikzpicture}


\end{document}

But I don't think that this is the most elegant solution.

Output:

Output

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.