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]
\addplot [color=red] {x};
\addplot [color=green] {e^x-5/2*x};
\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}
Best Answer
To draw a dotted line, do
For the vertical line, the
x
coordinates are both2
. You can add suitable y-coordinates manually, or to automatically get the axis limits, use\pgfkeysvalueof{/pgfplots/ymin}
and similar forymax
.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
Two things to note:
axis
environment.\pgfplotsset{compat=1.12}
the axis coordinates are used as default for\draw
and similar things inside anaxis
. Had you had acompat
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.