# [Tex/LaTex] Drawing dotted line on a plot

pgfplotstikz-pgf

I am trying to draw vertical and horizontal dotted lines on a plot with two functions and apparently I can't get the code necessary to work. I need the lines to be where n = 2 and f(n) = 2. Can anyone please show me how it's done?

\documentclass[11pt,a4paper,oneside]{article}
\usepackage{fullpage}
\usepackage[italian]{babel}
\usepackage[utf8]{inputenc}
\usepackage{amsfonts}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsthm}
\usepackage{color}
\usepackage{pgfplots}
\usetikzlibrary{positioning}
\usepackage[font={footnotesize,it},width=.5\textwidth]{caption}
\pgfplotsset{compat=1.12}

\begin{document}

\begin{figure}[hb]
\centering
\begin{tikzpicture}
\begin{axis}[domain=0:5, samples=50,grid=major,
legend style={legend cell align=left, font=\tiny},
restrict y to domain=0:5,xlabel=$\text{Dimensione dell'input}$,
ylabel=$\text{Tempo di esecuzione}$,
legend pos=north west]

\legend{$n$, $e^n-\frac{5}{2}n$}
\end{axis}
\end{tikzpicture}
\caption{In figura $f(n) = n$ e $g(n) = e^n-\frac{5}{2}n$.}
\label{gif:asymsup}
\end{figure}

\end{document}


To draw a dotted line, do

\draw [dotted] (x1,y1) -- (x2,y2);


For the vertical line, the x coordinates are both 2. You can add suitable y-coordinates manually, or to automatically get the axis limits, use \pgfkeysvalueof{/pgfplots/ymin} and similar for ymax.

For the horizontal line, use the fact that calculations are allowed in coordinates, so you can use e^2-5/2*2 for the y-coordinates directly. The x-values are obtained in the same way as mentioned above.

Hence, something like

\draw [very thick,dotted] (2,\pgfkeysvalueof{/pgfplots/ymin}) -- (2,\pgfkeysvalueof{/pgfplots/ymax});
\draw [very thick,dotted] (\pgfkeysvalueof{/pgfplots/xmin},e^2-5/2*2) -- (\pgfkeysvalueof{/pgfplots/xmax},e^2-5/2*2);


Two things to note:

1. Add the above commands inside the axis environment.
2. As you have \pgfplotsset{compat=1.12} the axis coordinates are used as default for \draw and similar things inside an axis. Had you had a compat setting of lower than 1.11, or none at all, you would have to use (axis cs:x,y) to specify that the axis coordinate system should be used.

\documentclass[margin=5pt]{standalone}
\usepackage{pgfplots,amsmath}
\pgfplotsset{compat=1.12}
\begin{document}
\begin{tikzpicture}
\begin{axis}[domain=0:5, samples=50,grid=major,
legend style={legend cell align=left, font=\tiny},
restrict y to domain=0:5,xlabel=$\text{Dimensione dell'input}$,
ylabel=$\text{Tempo di esecuzione}$, legend pos=north west]

\legend{$n$, $e^n-\frac{5}{2}n$}

\draw [very thick,dotted] (2,\pgfkeysvalueof{/pgfplots/ymin}) -- (2,\pgfkeysvalueof{/pgfplots/ymax});
\draw [very thick,dotted] (\pgfkeysvalueof{/pgfplots/xmin},e^2-5/2*2) -- (\pgfkeysvalueof{/pgfplots/xmax},e^2-5/2*2);
\end{axis}
\end{tikzpicture}
\end{document}