pgfplots y=x 图形未呈现所有点

pgfplots y=x 图形未呈现所有点
        \begin{tikzpicture}[baseline={([yshift=-1em] current bounding box.north)}]
            \begin{axis}
            [
                x = 0.75cm,
                y = 0.75cm,
                xlabel = $x$,
                ylabel = $y$,
                xmin = -10,
                xmax = 10,
                restrict x to domain = -10:10,
                ymin = -10,
                ymax = 10,
                restrict y to domain = -10:10,
                axis x line = middle,
                axis y line = middle,
            ]
                \addplot {x};
                \legend{$y = x$};
            \end{axis}
        \end{tikzpicture}

失败

为什么图表只渲染从 (-5,-5) 到 (5,5) 的图形?它应该是 (-10,-10) 到 (10,10)

答案1

restrict <x/y> to domain不设置用于计算y值的域,该域由键完成。 的domain默认值为,这意味着函数的求值范围为 -5 <= x <= 5。domain-5:5

pgfplots以下是来自手册中关于按键的引文restrict

这些键附加X(或者或者) 坐标过滤器将相应坐标限制在某个域内。不带星号的版本(如 )将在坐标低于 时restrict x to domain分配值,如果坐标高于 时分配值。带星号的版本(如)将把坐标截断为,即,如果坐标超出下限,并且值超出上限,则分配值。-infmin+infmaxrestrict x to domain*[min, max]minmax

手册中的示例包含多个周期的 tan(x) 图。

\documentclass[tikz,border=5mm]{standalone}
\usepackage{pgfplots}    
\begin{document}    
\begin{tikzpicture}
\begin{axis}[
restrict y to domain=-10:10,
samples=1000,
% some fine-tuning for the display:
width=10cm, height=210pt,
xmin=-4.7124, xmax=4.7124,
xtick={-4.7124,-1.5708,...,10},
xticklabels={$-\frac32 \pi$,$-\pi/2$,$\pi/2$,$\frac32 \pi$},
axis x line=center,
axis y line=center]
\addplot[blue] gnuplot[id=tangens,domain=-1.5*pi:1.5*pi]{tan(x)};

\legend{$\tan(x)$}
\end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

因此,对于您的示例,restrict ...实际上不需要键,并且为了获得 -10 < x 10 的图,请添加domain=-10:10

\documentclass[tikz,border=5mm,convert={false,density=100}]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}[baseline={([yshift=-1em] current bounding box.north)}]
    \begin{axis}
    [
     x = 0.75cm,
     y = 0.75cm,
     xlabel = $x$,
     ylabel = $y$,
     xmin = -10,
     xmax = 10,
     ymin = -10,
     ymax = 10,
     domain=-10:10,
     axis lines = middle,
    ]
\addplot {x};
\legend{$y = x$};
\end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容