pgfplot 不会绘制过去的 ax 值,绘图被截断

pgfplot 不会绘制过去的 ax 值,绘图被截断

我尝试绘制以下函数;但是,它们在 x=[-5,5] 处被截断,我不知道为什么。任何帮助都将不胜感激。

\documentclass{article}
\usepackage{pgfplots}

\usepgfplotslibrary{external}
\tikzexternalize[shell escape=-enable-write18] %needed for the MiKTeX compiler
\tikzset{external/force remake}

\begin{document}
    \begin{tikzpicture}
        \begin{axis}[xmin=-10,xmax=10]
            \addplot [mark=none, smooth, color=blue]{x^2+2};
            \addplot [mark=none, smooth, color=red]{x^2};
            \addplot [mark=none, smooth, color=green]{x^2-2};
            \addlegendentry{$x^2+2$}
            \addlegendentry{$x^2$}
            \addlegendentry{$x^2-2$}
        \end{axis}
    \end{tikzpicture}

    \begin{tikzpicture}
            \begin{axis}[xmin=-10,xmax=10]
                \addplot [mark=none, smooth, color=blue]{(x+2)^2};
                \addplot [mark=none, smooth, color=red]{x^2};
                \addplot [mark=none, smooth, color=green]{(x-2)^2};
                \addlegendentry{$(x+2)^2$}
                \addlegendentry{$x^2$}
                \addlegendentry{$(x-2)^2$}
            \end{axis}
    \end{tikzpicture}
\end{document}

答案1

您需要告诉每个\addplot要绘制的域,即

\addplot [mark=none, smooth, color=blue, domain=-10:10]{x^2+2};

正如 Knabber 所指出的,一种快捷方式是将域边界放在轴选项中,即

\begin{axis}[xmin=-10,xmax=10, domain=-10:10]
  \addplot [mark=none, smooth, color=blue]{x^2+2};
  \addplot [mark=none, smooth, color=red]{x^2};
  \addplot [mark=none, smooth, color=green]{x^2-2};
  ...
\end{axis}

最后,如果你不喜欢重复,你可以这样做

 \begin{axis}[enlargelimits=false, domain=-10:10]

其中,enlargelimits=false轴边界紧贴域(无填充)。

相关内容