使用宽度时,pgfplot 中出现神秘的“尺寸太大”错误

使用宽度时,pgfplot 中出现神秘的“尺寸太大”错误

看看以下 MWE:

\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots}\pgfplotsset{compat=1.9}
\begin{document}
\begin{tikzpicture}[]
    \begin{axis}[
        width=14cm, height=9cm,
        xmin=-1, xmax=1, 
        ymin=0, ymax=5,
        axis y line* = left,
        xlabel = {Nivel $L$ (m)},
        ylabel = {$y$},
        samples = 100,
        ]
        \addplot[thick, blue] {2.5*(4+x)-7.5}; 
        \label{plot:lin}
        \addplot[thick, red] {2.5*(4+0.7*x+0.3*x^3)-7.5}; 
        \label{plot:nl}
    \end{axis}        
    \begin{axis}[
       width=14cm, height=9cm,
        xmin=-1, xmax=1, 
        ymin=-1, ymax=1,
        axis y line* = right,
        axis x line* = none,
        ylabel = {absolute error $\epsilon$ (m)},
        samples = 100,
        ]
        % http://tex.stackexchange.com/a/42752/38080
        \addlegendimage{/pgfplots/refstyle=plot:lin}\addlegendentry{ideal}
        \addlegendimage{/pgfplots/refstyle=plot:nl}\addlegendentry{real}
        %
        \addplot[thick, green] {2.5*(4+0.7*x+0.3*x^3)-2.5*(4+x)};
        \addlegendentry{error}
    \end{axis}        
\end{tikzpicture}
\end{document}

编译时,会出现“尺寸太大”的错误:

! Dimension too large.
<recently read> \pgf@yy 

l.35     \end{axis}
[...]
? 
! Dimension too large.
\pgfusepath ...@y by.5\pgflinewidth \ifdim \pgf@y 
                                                  >\pgf@picmaxy \global \pgf...
l.35     \end{axis}

...但图表显示正确。

输出图

我究竟做错了什么?

PS:问题好像是height---如果超过7cm的话就会堵塞。

答案1

发生这种情况的原因是,即使你设置了xmin=-1, xmax=1,函数仍然会在更大的域(-5:5)上进行求值。轴限制之外的样本不会显示。这会产生很多不必要的开销,因此你应该通过设置 来限制域domain=-1:1

\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots}\pgfplotsset{compat=1.9}
\begin{document}
\begin{tikzpicture}[]
    \begin{axis}[
        width=14cm, height=9cm,
        xmin=-1, xmax=1, 
        domain=-1:1,
        ymin=0, ymax=5,
        axis y line* = left,
        xlabel = {Nivel $L$ (m)},
        ylabel = {$y$},
        samples = 100,
        ]
        \addplot[thick, blue] {2.5*(4+x)-7.5}; 
        \label{plot:lin}
        \addplot[thick, red] {2.5*(4+0.7*x+0.3*x^3)-7.5}; 
        \label{plot:nl}
    \end{axis}        
    \begin{axis}[
       width=14cm, height=9cm,
        xmin=-1, xmax=1,
        domain=-1:1,
        ymin=-1, ymax=1,
        axis y line* = right,
        axis x line* = none,
        ylabel = {absolute error $\epsilon$ (m)},
        samples = 100,
        ]
        % http://tex.stackexchange.com/a/42752/38080
        \addlegendimage{/pgfplots/refstyle=plot:lin}\addlegendentry{ideal}
        \addlegendimage{/pgfplots/refstyle=plot:nl}\addlegendentry{real}
        %
        \addplot[thick, green] {2.5*(4+0.7*x+0.3*x^3)-2.5*(4+x)};
        \addlegendentry{error}
    \end{axis}        
\end{tikzpicture}
\end{document}

相关内容