[Tex/LaTex] Restrict function plot to a range of x values using pgfplots.

pgfplotstikz-pgf

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},
]
\addplot[draw=red]{2/(1+exp(-x))-1};
\addplot[draw=blue,domain=0:4]{1/(1+exp(-x))};
\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.

Best Answer

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

\addplot[draw=blue][domain=0:4]{1/(1+exp(-x))};
Related Question