# Restrict function plot to a range of x values using pgfplots.

I want to create a plot of two functions using pgfplots and LaTeX. I want one of the functions only in a certain interval, I'll give an example:

\begin{tikzpicture}
\begin{axis}[
axis x line=middle,
axis y line=middle,
axis on top,
tick label style={font=\tiny},
smooth,
xlabel=$x$,
xmin=-4, xmax=4,
ymin=-1.5, ymax=1.5,
width=0.8\textwidth,
height=0.4\textwidth,
legend style={at={(0.02,0.97)},anchor=north west},
]
\end{axis}
\end{tikzpicture}

Now what I want is that the second, blue function is only drawn in the range x=0:4, but the output contains the graph for the whole intervall from -4 to 4. I've scrolled through the whole pgf manual, but it seems that domain=a:b is the solution. Maybe my PGF is out of date? I'm using Debian Squeeze with PGF version 2.0.

EDIT: I've tried out several examples from the PGF manual, and many don't work. I believe this is a problem with my machine/Debian/whatever and not with PGF. My current workaround is to generate a table of function values for each graph and include it with \addplot file {function_values.dat}; For example, my PGF ignores the samples=n setting and many graphs look horrible. Using a table of function values improves output considerably.

-

## migrated from stackoverflow.comFeb 11 '11 at 16:06

This question came from our site for professional and enthusiast programmers.

Which version of pgf manual is the manual for? The examples from the manual for v. 2.0 should work fine with your pgf v. 2.0. I never had any problems with pgf on Squeeze, although I now have manually installed the newest version. –  Jan Hlavacek Feb 11 '11 at 17:18
@Jan: I tested it, the example doesn't work with v2.0 from the Debian packages (at least on Ubuntu, but I believe they just import the TeX packages from Debian), but works with v2.10 from TL2010. –  Caramdir Feb 11 '11 at 17:20
@Caramdir: it seems that it's not the pgf version but the pgfplots version that matters here. –  Jan Hlavacek Feb 11 '11 at 18:23
another tip: It's not only good to keep the answer space reserved for actual answers, but also to keep the question reserved for the actual question. Your second edit is more of an answer, so you could indeed post it as an answer to your own question. (Moreover, it is really nice of you that want to express your thanks by words, but this is unusual here on tex.sx.) –  Hendrik Vogt Feb 12 '11 at 15:33

It seems that the problem with your example is that you are mixing tikz keys with pgf keys. Try this instead:

-
+1 Incredible -- but this one works! Just tested. –  hochl Feb 12 '11 at 14:17

As you have correctly assumed, your versions of PGF and pgfplots is outdated. The current pgfplots version is 1.4.1, where your example compiles correctly. It is contained in TeX Live 2010. Unfortunately Debian still only has packages for TeX Live 2009 (even in unstable) and hence only has pgfplots 1.2.2. (The pgf versions are 2.10 versus 2.00.)

The easiest solution to get updated packages is to install TeX Live 2010 manually. TeX Live has precompiled binaries, so the installation is quick and painless (well, “quick” of course depends on your internet connection). Should you need any help, please ask a separate question for that (but given that you are a Debian user, I doubt that you need any help with that).

-
Actually, I have 2.10, and the example does not compile correctly there, either, at least not as it is written. Se a simple adjustment in my answer that makes it compile correctly. –  Jan Hlavacek Feb 11 '11 at 17:21
@Jan: Interesting. I compiles correctly on my machine (Ubuntu 10.10, TL2010 (updated recently), compiling with 32bit pdflatex). –  Caramdir Feb 11 '11 at 17:24
It works fine on my machine with PGF 2.1 as well, and interestingly also on scribtext, which uses PGF 2.0. @Jan –  Jake Feb 11 '11 at 17:29
I think the mystery is solved. It is not the version of pgf that matters, but the version of pgfplots. I had the new pgf, but old pgfplots. Now that I upgraded pgfplots, the example works. –  Jan Hlavacek Feb 11 '11 at 18:20
@hochl: If you don't feel like upgrading the whole TeX Live, you can also upgrade just pgf and pgfplots. They both come as zipfiles with TDS compliant directory structure, all you need to do is unzip them in /usr/local/share/texmf/ and run texhash as a root. –  Jan Hlavacek Feb 11 '11 at 18:28

Now I draw my graph using