Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I tried plotting the following functions; however, they are cut off at x=[-5,5] and I cannot figure out why. Any help would greatly be appreciated.

\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}
share|improve this question

1 Answer 1

up vote 3 down vote accepted

You need to tell each \addplot the domain to plot over, i.e.

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

As noted by Knabber, a short-cut is to place the domain boundaries in the axis options, i.e.

\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}

Finally, if you don't like repetition you can do

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

where the enlargelimits=false makes the axis boundary hug the domain (no padding).

share|improve this answer
    
\begin{axis}[xmin=-10,xmax=10,domain=-10:10] will set the domain for all \addplot –  dozedoff Jan 4 '12 at 13:51
    
@Knabber: Thanks, added a note on this. –  qubyte Jan 4 '12 at 13:54

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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