在 pgfplots 中,如何填补对称图接近零的间隙?

在 pgfplots 中,如何填补对称图接近零的间隙?

我正在绘制该方程的简单图,因此在和9x^2=x+4上绘制了两个图。sqrt(x+4)/3-sqrt(x+4)/3

在草稿编译期间,我使用 40 个样本,在最终编译期间使用 400 个。使用 40 个样本会产生很大差距;样本数量越多,差距就越小,但可计算的样本数量是有限的。

我忽略了什么方法来填补这两个\addplot命令之间的空白?

%%% mwe.

\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{figure}\centering
\begin{tikzpicture}
    \begin{axis}
    \addplot[
        samples = 400,
        thick,
    ]
    {sqrt(x+4)/3)};
    \addplot[
        samples = 400,
        thick,
    ]
    {-sqrt(x+4)/3)};
    \end{axis}
\end{tikzpicture}
\caption{$9y^2=x+4$. How does one fill the gap at $(-4,0)$?}
\label{fig:c02f08}
\end{figure}
\end{document}

答案1

您缺少默认域,即-5:5。如果样本数量为偶数,您将无法获得 x=-4 的值。例如samples=401,您可以使用 或samples=400,domain=-4:5。后者可能更好,因此pgfplots在尝试计算负数的平方根时不必丢弃这些值。(当pgfplots遇到像 sqrt(-1) 这样的值时,它会在日志文件中打印警告。可以使用以下方法关闭这些警告\pgfplotsset{filter discard warning=false},但在这种情况下,我认为最好只设置一个适当的域。您可以在第 4.5.13 节中阅读有关无界坐标处理的更多信息 中断的剧情请参阅手册。

我所说的样本数量是指,如果您有 10 个样本,范围从 -5 到 5,那么值之间的距离将是 10/9,因此您会在 -5 处获得一个样本,在 -3.9 处获得下一个样本。对于大量样本,情况也是如此。

在此处输入图片描述

\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{figure}\centering
\begin{tikzpicture}
    \begin{axis}
    \addplot[
        samples = 400,domain=-4:5,
        thick,
    ]
    {sqrt(x+4)/3)};
    \addplot[
        samples = 400,domain=-4:5,
        thick,
    ]
    {-sqrt(x+4)/3)};
\end{axis}
\end{tikzpicture}
\caption{$9y^2=x+4$. How does one fill the gap at $(-4,0)$?}
\label{fig:c02f08}
\end{figure}
\end{document}

相关内容