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'm just starting to learn TikZ/PGF. I would like to draw a simple function, f(x) = x² to start with.

\documentclass{article}

\usepackage{tikz}
\usepackage{pgfplots}


\begin{document}
\begin{tikzpicture}
\begin{axis}

\draw [thick, domain=0:1] plot  (\x,  {\x*\x});


\end{axis}
\end{tikzpicture}
\end{document}

This is the most minimal example I can think of. Substituting the \x*\x with something else to exclude a syntax error does not work either. Where am I making the mistake?

share|improve this question

2 Answers 2

up vote 9 down vote accepted

Change \draw [thick, domain=0:1] plot (\x,{\x*\x}); to \addplot [thick, domain=0:1] (\x,{\x*\x}); and it should work fine

share|improve this answer
    
Thanks a lot, what a stupid mistake. –  Eekhoorn Aug 3 '12 at 9:33
    
No worries, we all make simple mistakes at the beginning. And you'll learn more by it...Claudio's answer is probably more insightful though as it explains the mix up. –  Leeser Aug 3 '12 at 9:37

I think you are mixing the syntax from both TikZ and pgfplots. Basically, if you want to use pgfplots, you need to call \addplot inside the axis environment, while when using TikZ you simply need the standard \draw command with plot.

Here you find two basic examples in both cases with your function:

\documentclass{article}
\usepackage{pgfplots}
\begin{document}
Using just Ti\emph{k}Z:
\begin{center}
\begin{tikzpicture}[ domain=0:1]
\draw[very thin,color=gray] (-0.1,-1.1) grid (1.9,1.9);
\draw [thick,] plot  (\x,\x*\x);
\end{tikzpicture}
\end{center}
\vspace{2cm}
Using pgfplots:
\begin{center}
\begin{tikzpicture}[domain=0:1]
\begin{axis}
\addplot {x*x};
\end{axis}
\end{tikzpicture}
\end{center}
\end{document}

Result:

enter image description here

Notice that in my example I just loaded pgfplots since it automatically loads TikZ.

share|improve this answer

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.