1

This is the plot I'm working on:

\documentclass{book}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        axis lines=center,
        samples=200,
        xlabel={\(x\)},
        xmin=-0.5,xmax=3.5,
        ylabel={\(y\)},
        ymin=-0.5,ymax=2.5,
    ]
        \addplot [green] {(x - 2)^3 + 1};
    \end{axis}
\end{tikzpicture}
\end{document}

If I use ymin=-1.5 or any negative value smaller than -1, the plot renders just fine. However, it complains that Dimension too large when I use ymin=-0.5 as above.

I read the pgfplots docs, section 2.6.2, and still can't find the solution.

7
  • 2
    Yo need to define domain of the your function, for example \addplot [domain=-1.5:3] {(x - 2)^3 + 1}; works fine ... – Zarko Oct 17 '20 at 6:05
  • @Zarko thanks! I skimmed the first 50 pages of the docs again and still can't find that domain is required... – Minh Nghĩa Oct 17 '20 at 6:37
  • 2
    It's not generally required, as it has a default value (-5:5). I don't know why it crashes with your example though, seems a bit odd ... – Torbjørn T. Oct 17 '20 at 7:32
  • @TorbjørnT., with default domain the values of function becomes to big. If you add restrict y to domain=0:12 to axis option, than it works. I will write an answer asap. – Zarko Oct 17 '20 at 8:52
  • @Zarko That cannot be the problem, if you set ymin=-1.5 as Minh said, or remove ymin/ymax and xmin/xmax altogether, it works perfectly fine. – Torbjørn T. Oct 17 '20 at 8:57
2

If you use default domain, i.e. domain=-5:5, than values of your function seems become becomes to big. This is somehow odd, see @ Torbjørn T. 1 comment.

To resolve this, you have two option:

  • define domain between xmin and xmax (it has not sense to use wider domain), or
  • define restriction for y (function) values:
\documentclass{book}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}

\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        axis lines=center,
        xlabel={\(x\)},
        xmin=-0.5,xmax=3.5,
        ylabel={\(y\)},
        ymin=-0.5,ymax=2.5,
        samples=200,
        restrict y to domain=-0.5:12,
        domain=-0.5:3.5,        
every axis plot post/.append style={very thick},        
    no marks
    ]
        \addplot    {(x - 2)^3 + 1};
    \end{axis}
\end{tikzpicture}
\end{document}

second case:

\documentclass{book}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}

\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        axis lines=center,
        xlabel={\(x\)},
        xmin=-0.5,xmax=3.5,
        ylabel={\(y\)},
        ymin=-0.5,ymax=2.5,
        samples=200,
        restrict y to domain=-0.5:12,
every axis plot post/.append style={very thick},        
    no marks
    ]
        \addplot    {(x - 2)^3 + 1};
    \end{axis}
\end{tikzpicture}
\end{document}

in the both cases the result is the same.

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.