在 pgfplots 中填充两个参数路径(例如圆)之间​​的交点

在 pgfplots 中填充两个参数路径(例如圆)之间​​的交点

我尝试使用fillbetween库来pgfplots填充两个圆的交点。但是我得到的却是“反转”颜色:

\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}

输出:

输出

白色区域应为蓝色,蓝色区域应为白色。为什么 pgfplots 会这样填充?我该如何修复它?

答案1

您可以使用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}

在此处输入图片描述

显示intersection segments

在此处输入图片描述

因此,需要绿色 (L0)、橙色 (L1) 和黄色 (R2) 段来sequence填充两个圆圈的交点。

代码:

\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

感谢@fpast发布的链接,以下代码有效:

\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}

但我不认为这是最优雅的解决方案。

输出:

输出

相关内容