7

I am using TikZ and pgfplots for the first time. (I am using it so that I can send class notes to students in a Calculus class that I am teaching.) I have a graph with a slant asymptote. Before I can use pgfplots for the graph, I have some adjustments needed for just the lines in the graph!

I saw in another post that shorten <=2ex, shorten >=2ex would shorten a line at each end by 2ex. This command shortened the vertical line but not the slanted line. I want arrowheads on both lines; they are on the vertical line but not the slanted line. I want the lines labeled in the same direction as the lines ... but off the line. So, I need to move one label up and one label to the right. Since arctan(3/2) = 56.31 degrees, I used rotate=56.31 in the options for the node. Is there a "slant" command for drawing such labels in the same direction as the line? One more thing. The label "y" for the y-axis is too close to the vertical line x=3/2. How do I adjust its position? (I don't like the position of the "x" for the x-axis, too.)

\documentclass{amsart}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}


\begin{document}    

\begin{tikzpicture}
\begin{axis}[width=4in,axis equal image,
    axis lines=middle,
    xmin=-15,xmax=15,
    xlabel=$x$,ylabel=$y$,
    ymin=-10,ymax=10,
    restrict y to domain=-12:12,
    enlargelimits={abs=1cm},
    axis line style={latex-latex},
    ticklabel style={font=\tiny,fill=white},
    xtick={\empty},ytick={\empty},
]

\draw[dashed,latex-latex,shorten <=2ex, shorten >=2ex] (-13,-17) -- (13,22) node[right,rotate=56.31,pos=0.1]{$\scriptstyle{y=(3/2)x+5/2}$};
\draw[dashed,latex-latex,shorten <=2ex, shorten >=2ex] ({1.5,0} |- {{axis description cs:1,1}}) -- ({{1.5,0}} |- {{axis description cs:1,-1}}) node[right,rotate=90,pos=0.5]{$\scriptstyle{x=3/2}$};
\end{axis}
\end{tikzpicture}

\end{document}
2

2 Answers 2

10

The reason why the shorten commands don't seem to work reliably is because you can't see the ends of your lines: they're outside the plot area, and are clipped off. To see the full lines, add clip=false to your axis options.

I would approach this differently: instead of using \draw commands, you can use "real" plots with the \addplot command. That way, you can specify the equations directly, instead of having to calculate the angle for the rotation beforehand.

\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}


\begin{document}    

\begin{tikzpicture}
\begin{axis}[width=4in,axis equal image,
clip=false,
    axis lines=middle,
    xmin=-15,xmax=15,
    domain=-15:15, samples=50,
    xlabel=$x$,ylabel=$y$,
    ymin=-10,ymax=10,
    restrict y to domain=-12:12,
    enlargelimits={abs=1cm},
    axis line style={latex-latex},
    ticklabel style={font=\tiny,fill=white},
    xtick={\empty},ytick={\empty},
]

\addplot [dashed, latex-latex] {(3/2)*x+5/2} node [pos=0.25, anchor=south, font=\footnotesize, sloped] {$y=(3/2)x+5/2$};
\addplot [dashed, latex-latex] (1.5,x) node [pos=0.25, anchor=north, font=\footnotesize, sloped] {$x=3/2$};
\end{axis}
\end{tikzpicture}

\end{document}
2
  • 1
    Nice! I had not seen the command \addplot (1.5,x) for drawing vertical lines. Is that in the manual? Yes, I like the sloped option for the \addplot command. I think the only modifications that I need are the positions to the "x" and "y" on the axes.
    – Adelyn
    Sep 26, 2014 at 17:59
  • 2
    @Adelyn: \addplot (<x>, <y>) is the syntax for drawing parametric plots. See section 4.3.3 "Computing Coordinates with Mathematical Expressions" of the current manual for more info (it can be a bit overwhelming at first...)
    – Jake
    Sep 26, 2014 at 18:02
7

You can use the sloped option instead of computing the angle:

enter image description here

Notes:

  • I also replaced the slat fractions with a \frac as I think that looks much better.
  • To get the axis labels at the tips you can use the Axis Labels At Tip style that I extracted from How to properly scale a TikZ/PGF picture which has a `\begin{axis}...\end{axis}`.
  • The reason why there were not arrowheads on both ends can be seen if you set clip=false. The lines were way beyond the displayed range. So you can either edit the coordinates to get the end points of the lines into range or just increase the amount you shorten the line with. In the MWE below I increased the shorten amounts as that was simpler than computing appropriate coordinates to maintain the same slope and position.

Code:

\documentclass{amsart}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}

%% https://tex.stackexchange.com/questions/17438/how-to-properly-scale-a-tikz-pgf-picture-which-has-a-beginaxis-endaxis
%%
\pgfkeys{/pgfplots/Axis Labels At Tip/.style={
        xlabel style={
            at={(current axis.right of origin)}, 
            xshift=1.5ex, anchor=center
        },
        ylabel style={
            at={(current axis.above origin)}, 
            yshift=1.5ex, anchor=center
        }
    }
}

\begin{document}    

\begin{tikzpicture}
\begin{axis}[width=4in,axis equal image,
    axis lines=middle,
    xmin=-15,xmax=15,
    xlabel=$x$,ylabel=$y$,
    ymin=-10,ymax=10,
    restrict y to domain=-12:12,
    enlargelimits={abs=1cm},
    axis line style={latex-latex},
    ticklabel style={font=\tiny,fill=white},
%    xtick={\empty},ytick={\empty},
    Axis Labels At Tip,
    %clip=false
]

\draw[dashed,latex-latex,shorten <=4ex, shorten >=15ex] (axis cs: -13,-17) -- (axis cs: 13,22) node[right,sloped, above,pos=0.15]{$y=\frac{3}{2}x+ \frac{5}{2} $};
\draw[dashed,latex-latex,shorten <=2ex, shorten >=2ex] ({1.5,0} |- {{axis description cs:1,1}}) -- ({{1.5,0}} |- {{axis description cs:1,0}}) node[right,rotate=90, below, pos=0.65]{$x= \frac{3}{2}$};
\end{axis}
\end{tikzpicture}

\end{document}
7
  • 1
    I appreciate your response. You were the first one to reply to me on this site! I have two comments on your code. Why don't your lines have arrowheads? The only differences between your code and Jake's code is he uses clip=false and \addplot. I also want to move the labels "x" and "y" on the axes. I looked at another posting. This is the code: xlabel=$x$,ylabel=$\sin x$, every axis x label/.style={at={(ticklabel* cs:1.05)},anchor=west,}, every axis y label/.style={at={(ticklabel* cs:1.05)},anchor=south,} How do I get "x" below and to the right of right arrowhead?
    – Adelyn
    Sep 27, 2014 at 12:54
  • 1
    How do I get "y" above and to the right (or to the left) of the top arrowhead?
    – Adelyn
    Sep 27, 2014 at 12:55
  • 2
    @Adelyn: Both your comments should now be addressed in the revised solution. Sep 27, 2014 at 19:58
  • 1
    @ Peter Grill Thanks! I did not have time to find this in the manual. I will look in the manual for the description of the Axis Labels At Tip command later. The axes for all the graphs in this particular LaTeX file should look the same. So, it is convenient to have this code.
    – Adelyn
    Sep 27, 2014 at 22:22
  • 1
    @Adelyn: As mentioned in the answer, Axis Labels At Tip is from How to properly scale a TikZ/PGF picture which has a \begin{axis}…\end{axis}. It is not a style that is in the documentation. But xlabel style and ylabel style should be in the documentation. Sep 28, 2014 at 1:29

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.