Wrong pgfplots plot for function with vertical asymptote

I want to plot the function $\frac{3 x}{1 + x}$ with pgfplots. Using

\begin{tikzpicture}
\begin{axis}[axis x line=none, axis y line=none, no markers]
\addplot {3 * x / (1 + x)};
\end{axis}
\end{tikzpicture}


I get this:

This is obviously wrong. The same function plotted in Grapher.app:

-
For me this seems to be a bug of pgfplots. Is there already a bug-report somewhere about this? –  student Sep 29 '13 at 18:02

2 Answers

You can set restrict y to domain=-20:20. That will discard all coordinates that are outside that range, and the plot will be interrupted. You should make sure that the specified range is somewhat larger than the visible axis limits, otherwise the plot might not extend all the way to the edges.

\documentclass[a4paper,12pt, border=5mm]{standalone}
\usepackage{pgfplots}

\begin{document}%
\begin{tikzpicture}
\begin{axis}[
axis lines*=middle,
no markers,
ymin=-5, ymax=10,
enlarge y limits=true,
enlarge x limits=false,
restrict y to domain=-20:20]
\addplot [thick, samples=50, smooth] {3 * x / (1 + x)};
\end{axis}
\end{tikzpicture}
\end{document}

-

One possible (not very elegant) solution is to split the plot at the asymptote:

\begin{tikzpicture}
\begin{axis}[
height=10cm,
width=10cm,
xmin = -5,
xmax = 5,
ymin=-10,
ymax=10,
grid=major,
samples=100,
enlargelimits=false,
legend pos= north east
]

\addplot+[mark=none, smooth,domain=-10:-1.05,draw=blue] {(3 * \x) / (1 +
\x)};
\addplot+[mark=none, smooth,domain=-0.95:10,draw=blue] {(3 * \x) / (1 + \x)};

\end{axis}
\end{tikzpicture}


Ideally, I would like something that works automatically, without needing to know the asymptote.

-
I think it's better if you state your requirements/wishes in your question, not in your answer. –  Marc van Dongen Dec 18 '12 at 9:44