极坐标曲线阴影错误与填充之间

极坐标曲线阴影错误与填充之间

因此,我尝试在内部r = 6 sin (\theta)和外部的两个极坐标曲线之间进行着色r = 2 + 2 sin(\theta)。使用 tikzfillbetween,我希望只对新月进行着色,但结果发现阴影区域非常抽象,不是我喜欢的样子。

有人能改进我的代码,让我可以在笛卡尔系统中阴影化区域吗?希望可以使用 fillbetween 或任何简单易懂的方法来实现。

这是我的 MWE:

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{fillbetween}
\begin{document}
\begin{center}
\begin{tikzpicture}
    \begin{axis}[
        axis equal,
        axis x line = middle,
        axis y line = middle,
        xlabel = $x$, ylabel = $y$,
        xtick = {-5, 0, 5}, minor x tick num = 4,
        ytick = {-5, 0, 5, 10}, minor y tick num = 4,
        xmin = -3.8, xmax = 3.8,
        ymin = -1.8, ymax = 6.8,
        font = \tiny,
        legend cell align={left},
    ]
        \addplot[name path = A, data cs = polar, orange, smooth, thin, samples = 1000, domain = 0 : 360] (x, {6*sin(x)});
        \addplot[name path = B, data cs = polar, magenta, smooth, thin, samples = 1000, domain = 0 : 360] (x, {2 + 2*sin(x)});
        \addplot[gray!20, fill opacity = .3] fill between [of = A and B, soft clip = {domain = -2.6:2.6}];
        \addlegendentry{{\color{black} $r = 6\sin\theta$}}
        \addlegendentry{{\color{black} $r = 2 + 2\sin\theta$}}
    \end{axis}
\end{tikzpicture}
\end{center}
\end{document}

答案1

@Jasper Habicht 答案的一个很小的变体(+1),代码更短,字体更大,样本数量更少:

\documentclass[border=3pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\usepgfplotslibrary{fillbetween}

\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        axis lines = middle,
        axis equal,
        xlabel = $x$, ylabel = $y$,
        xtick = {-5, 0, 5}, minor x tick num = 4,
        ytick = {-5, 5},    minor y tick num = 4,
        xmin = -6,      xmax = 6,
        ymin = -0.8,    ymax = 6.8,
        font = \scriptsize,
        legend cell align={left},
        data cs = polar, 
        smooth, 
        samples=90, 
        domain=0:360
    ]
        \addplot[name path = A, orange] (x, {6*sin(x)});
        \addplot[name path = B, magenta] (x, {2 + 2*sin(x)});
        \fill[gray!20, opacity = .3, 
              intersection segments = {of = A and B,
              sequence = {L2 -- R2[reverse]},
        }] -- cycle;
        \addlegendentry{{\color{black} $r = 6\sin\theta$}}
        \addlegendentry{{\color{black} $r = 2 + 2\sin\theta$}}
    \end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

您可以使用带有选项的常规\fill命令intersection segments来绘制新月。此外,您可能应该减少值以samples加快编译过程:

\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{fillbetween}

\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        axis equal,
        axis x line = middle,
        axis y line = middle,
        xlabel = $x$, ylabel = $y$,
        xtick = {-5, 0, 5}, minor x tick num = 4,
        ytick = {-5, 0, 5, 10}, minor y tick num = 4,
        xmin = -3.8, xmax = 3.8,
        ymin = -1.8, ymax = 6.8,
        font = \tiny,
        legend cell align={left},
    ]
        \addplot[name path = A, data cs = polar, orange, smooth, thin, samples = 250, domain = 0 : 360] (x, {6*sin(x)});
        \addplot[name path = B, data cs = polar, magenta, smooth, thin, samples = 250, domain = 0 : 360] (x, {2 + 2*sin(x)});
        \fill[gray!20, opacity = .3, intersection segments = {
            of = A and B,
            sequence = {L2 -- R2[reverse]},
        }] -- cycle;
        \addlegendentry{{\color{black} $r = 6\sin\theta$}}
        \addlegendentry{{\color{black} $r = 2 + 2\sin\theta$}}
    \end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容