使用 addplot 在 Tikz 中填充两个圆圈之间

使用 addplot 在 Tikz 中填充两个圆圈之间

我想绘制以下填充特定区域的图形

在此处输入图片描述

这是我的代码,我只得到了 x 轴右侧的结果。当我更改clip={domain=0:3}clip={domain=-3:3}它时,它不起作用

\begin{tikzpicture}
    \begin{axis}[axis lines=middle,axis on top,xlabel=$x$,ylabel=$y$,
    xmin=-5.5,xmax=5.5,ymin=-5.5,ymax=5.5,ytick={-5,-4,-3,-2,-1,1,2,3,4,5,6},yticklabels={$-5$,$-4$,$-3$,$-2$,$-1$,$1$,$2$,$3$,$4$,$5$,$6$},   xtick={-5,-4,-3,-2,-1,1,2,3,4,5,6},xticklabels={$-5$,$-4$,$-3$,$-2$,$-1$,$1$,$2$,$3$,$4$,$5$,$6$},width=\textwidth]   
    \addplot[domain=-3:3,samples=101,smooth,thick,name path=A]{sqrt(9-x^2)}  node {};
    \addplot[domain=-3:3,samples=101,smooth,thick,name path=B]{-sqrt(9-x^2)}  node {};
    \addplot[domain=-2:2,samples=101,smooth,thick,name path=C,color=white]{sqrt(4-x^2)}  node {};
    \addplot[domain=-2:2,samples=101,smooth,thick,name path=D,color=white]{-sqrt(4-x^2)}  node {};
    \addplot [gray!20] fill between [ of=C and A,soft clip={domain=0:3}];
    \addplot [gray!20] fill between [ of=D and B,soft clip={domain=0:3}];
    \end{axis}
    \end{tikzpicture}

答案1

我认为在这种情况下不这样做会更容易fillbetween。简单一点even odd rule就可以了。

\documentclass[tikz,border=3mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}
\begin{document}
\begin{tikzpicture}
 \begin{axis}[axis lines=middle,axis on top,xlabel=$x$,ylabel=$y$,
    xmin=-5.5,xmax=5.5,ymin=-5.5,ymax=5.5,
    ytick={-5,...,-1,1,2,...,6},
    yticklabels={$-5$,$-4$,$-3$,$-2$,$-1$,$1$,$2$,$3$,$4$,$5$,$6$},
    xtick={-5,...,-1,1,2,...,6},
    xticklabels={$-5$,$-4$,$-3$,$-2$,$-1$,$1$,$2$,$3$,$4$,$5$,$6$},   
    width=\textwidth,
    set layers]   
    \begin{pgfonlayer}{axis background}
     \fill[gray!20,even odd rule] (0,0) circle[radius=3] circle[radius=2]; 
    \end{pgfonlayer}
    % just to have some plot
    \addplot[domain=0:356.4,samples=101,smooth cycle]   ({3*cos(x)},{3*sin(x)});
 \end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

这是带有圆圈的版本。

\documentclass[tikz,border=3mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}
\begin{document}
\begin{tikzpicture}
 \begin{axis}[axis lines=middle,axis on top,xlabel=$x$,ylabel=$y$,
    xmin=-5.5,xmax=5.5,ymin=-5.5,ymax=5.5,
    ytick={-5,...,-1,1,2,...,6},
    yticklabels={$-5$,$-4$,$-3$,$-2$,$-1$,$1$,$2$,$3$,$4$,$5$,$6$},
    xtick={-5,...,-1,1,2,...,6},
    xticklabels={$-5$,$-4$,$-3$,$-2$,$-1$,$1$,$2$,$3$,$4$,$5$,$6$},   
    width=\textwidth,
    set layers,axis equal image]   
    \begin{pgfonlayer}{axis background}
     \fill[gray!20,even odd rule] (0,0) circle[radius=3] circle[radius=2]; 
    \end{pgfonlayer}
    % just to have some plot
    \addplot[domain=0:356.4,samples=101,smooth cycle]   ({3*cos(x)},{3*sin(x)});
 \end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容