7

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: Wrong plot using pgfplots

This is obviously wrong. The same function plotted in Grapher.app: Correct plot using OS X's 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
12

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}
|improve this answer|||||
2

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.

|improve this answer|||||
  • 1
    I think it's better if you state your requirements/wishes in your question, not in your answer. – user10274 Dec 18 '12 at 9:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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