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.

This question already has an answer here:

When I'm drawing f(x)=1/x or similar functions, pgfplots draws asymptotes. Do I have to draw the function in two pieces with different domains, or am I missing something?

enter image description here

\documentclass[a4paper]{article}
\usepackage{tkz-euclide}
\usepackage{pgfplots}
\usetkzobj{all}

\begin{document}
\begin{center}
\begin{tikzpicture}
\begin{axis}%
    [
        grid=major,  
        x=5mm,
        y=5mm,
        xtick={-10,-9,...,10},   
        xmin=-10,
        xmax=10,
        xlabel={\tiny $x$},
        axis x line=middle,
        ytick={-10,-9,...,10},
        tick label style={font=\tiny},
        ymin=-10,
        ymax=10,
        ylabel={\scriptsize $f(x)$},
        axis y line=middle,
        no markers,
        samples=100,
        domain=-10:10,
    ]
    \addplot[thick] (x,{1/x});
    \addplot[dashed] (x,{1/(x-2)});
\end{axis} 
\end{tikzpicture}
\end{center}
\end{document}
share|improve this question

marked as duplicate by Thorsten Donig, egreg, Jake, karlkoeller, percusse Aug 29 '13 at 10:36

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1  
It seems to be a duplicate. But using restrict y to domain=-20:20 does not seem to help with 1/x, with 3 * x / (1 + x) it works like a charm. Weird. –  moewe Aug 29 '13 at 10:17
    
Sorry for the duplicate. My native language isn't English... I was looking in the answers on similar questions, but none of the answers related to the questions that I had found was solving my problem. Next time I will look even longer. –  Arne Aug 29 '13 at 12:22

1 Answer 1

up vote 2 down vote accepted

You have to play a little with restrict y to domain and with the samples of the two plots. I don't know why but if the samples number is too low the asymptote is visible also with the restrict y to domain. If restrict y to domain is too low you have to increase a lot the sample number in order to have the graph extended until the edges of your plot.

But it is easier to play with these two values than to explain them whit words, so enjoy!

\documentclass[a4paper]{article}
\usepackage{tkz-euclide}
\usepackage{pgfplots}
\usetkzobj{all}

\begin{document}
\begin{center}
\begin{tikzpicture}
\begin{axis}%
    [
        grid=major,  
        x=5mm,
        y=5mm,
        xtick={-10,-9,...,10},   
        xmin=-10,
        xmax=10,
        xlabel={\tiny $x$},
        axis x line=middle,
        ytick={-10,-9,...,10},
        tick label style={font=\tiny},
        ymin=-10,
        ymax=10,
        ylabel={\scriptsize $f(x)$},
        axis y line=middle,
        no markers,
        samples=100,
        domain=-10:10,
        restrict y to domain=-20:20
    ]
    \addplot[thick,samples=400] (x,{1/x});
    \addplot[dashed, samples=400] (x,{1/(x-2)});
\end{axis} 
\end{tikzpicture}
\end{center}
\end{document}
share|improve this answer
    
strange, but it works, thanks –  Arne Aug 29 '13 at 11:05

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