6

I have the graph of a function bounded by 1. To emphasize that y=-1 and y=1 are the asymptotes to the graph of the function, I want the domain to be the interval [-30, 30] ... or something like that. The graph that pgfplots gives is about twice as long vertically as I want. How do I reduce this? I also want the display of the x-axis to be about twice as long as what pgfplots gives. How do I make it longer?

Also, I used fill=white in ticklabel style so that the horizontal asymptotes are not drawn over the labels for the tick marks. Why are the asymptotes drawn over the labels? (Why does tex.stackexchange tell me that "asymptotes" is misspelled?!)

\documentclass{amsart}
\usepackage{amsmath}
\usepackage{amsfonts}

\usepackage{pgfplots}
\pgfplotsset{compat=1.11}


\begin{document}

\begin{tikzpicture}
\begin{axis}[width=3.5in,clip=false,
    axis lines=middle,
    xmin=-30,xmax=30,
    ymin=-1.25,ymax=1.25,
    ticklabel style={font=\tiny,fill=white},
    xtick={\empty},ytick={\empty},
    extra y ticks={-1,1},
    extra y tick labels={$-1$,1},
    enlargelimits={abs=0.5cm},
    axis line style={latex-latex},
    xlabel=$x$,ylabel=$y$,
    xlabel style={at={(ticklabel* cs:1)},anchor=north west},
    ylabel style={at={(ticklabel* cs:1)},anchor=south west}
]
\addplot[samples=201,domain=-30:0,blue] {x / (- x + 1)} node[anchor=south east, pos=0.25,font=\footnotesize]{$y = \dfrac{x}{\vert x \vert + 1}$};
\addplot[samples=201,domain=0:30,blue] {x / (x + 1)};
\addplot[latex-latex,dashed,draw=gray!10,samples=2,domain=-30:30] {-1};
\addplot[latex-latex,dashed,samples=2,draw=gray!10,domain=-30:30] {1};

\end{axis}
\end{tikzpicture}

\end{document}

1 Answer 1

1

You can set the width of the graph to be twice the height to get the size that you want. I've drawn the asymptotes as two arrows pointing left and right to avoid being drawn over the labels, colored gray to differentiate them from the tick marks.

\documentclass{amsart}
\usepackage{amsmath}
\usepackage{amsfonts}

\usepackage{pgfplots}
\pgfplotsset{compat=1.11}


\begin{document}

\begin{tikzpicture}
\begin{axis}[width=5in,
    height=2.5in,
    clip=false,
    axis lines=middle,
    xmin=-30,xmax=30,
    ymin=-1.25,ymax=1.25,
    ticklabel style={font=\tiny},
    xtick={\empty},ytick={\empty},
    extra y ticks={-1,1},
    extra y tick labels={$-1$,1},
    enlargelimits={abs=0.5cm},
    axis line style={latex-latex},
    xlabel=$x$,ylabel=$y$,
    xlabel style={at={(ticklabel* cs:1)},anchor=north west},
    ylabel style={at={(ticklabel* cs:1)},anchor=south west}
]
\addplot[samples=201,domain=-30:0,blue] {x / (- x + 1)} node[anchor=south east, pos=0.25,font=\footnotesize]{$y = \dfrac{x}{\vert x \vert + 1}$};
\addplot[samples=201,domain=0:30,blue] {x / (x + 1)};

% Bottom asymptote
\addplot[-latex,dashed,gray,samples=2,domain=1:30] {-1};
\addplot[latex-,dashed,gray,samples=2,domain=-30:-3] {-1};
% Top asymptote
\addplot[-latex,dashed,gray,samples=2,domain=1:30] {1};
\addplot[latex-,dashed,gray,samples=2,domain=-30:-2] {1};
\end{axis}
\end{tikzpicture}

\end{document}

enter image description here

5
  • I should be able to put the label for the tick mark over the graph. Why doesn't fill=white work?
    – user74973
    Commented Sep 17, 2015 at 22:15
  • What do you mean by putting the label 'over' the graph? fill=white does work, it's just that white is the default color so there's no visible difference (if you use fill=red or something you'll see the effect).
    – JamesNZ
    Commented Sep 18, 2015 at 0:12
  • 1
    Ah, just realized you meant the graph of the asymptote :p The reason is simply that the labels/grid/ticks are drawn before the graph.
    – JamesNZ
    Commented Sep 18, 2015 at 0:19
  • Here is an option that can be added to the axis environment for pgfplots will do the computations so that the -1 and 1 are typeset above the axes: axis on top. With this option, you can just say that the domains of the functions for the horizontal lines are [-30, 30].
    – user74973
    Commented Sep 21, 2015 at 21:00
  • 1
    Aha, excellent.
    – JamesNZ
    Commented Sep 22, 2015 at 7:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .