[Tex/LaTex] Problem with axis when drawing a function plot using PGFplots

pgfplotstikz-pgf

Dear TeX StackExchange,

I am trying to plot two functions in the same grid. The code I currently have is below:

\begin{center}
\begin{tikzpicture}[scale=1, transform shape]
\begin{axis}[grid=both,
      mark = none,
      xmin = 0, ymin = 0,
      xmax = 16,ymax = 16,
      axis lines*=middle,
      enlargelimits,
      xtick={0,2,...,16},
      ytick={0,2,...,16}]
\addplot[red, domain=0:5, samples=100]  {pow(2,x)} node[pos=0.52, right]{$y=2^n$};
\addplot[blue, domain=0:18, samples=100]  {x} node[pos = 0.89, above left] {$y=n$};
\end{axis}
\end{tikzpicture}
\end{center}

The code below gets plotted as:

however, I would like the grey lines not to overlap the x- and y-axis, but the graph should not change otherwise – when I tried axis lines *= left, it got shifted a little bit, as can be seen here (which is something I do not want):

which is something I want to achieve partially (the axis are fixed), however the plots do not look like on the first picture (since they are shifted).

Any ideas?

Best Answer

You can use elargelimits=false or enlargelimits=upper to achieve your goal.

\documentclass{article}
\usepackage{pgfplots}
\begin{document}
  \begin{center}
\begin{tikzpicture}[scale=1, transform shape]
\begin{axis}[grid=both,
      mark = none,
      xmin = 0, ymin = 0,
      xmax = 16,ymax = 16,
      axis lines*=middle,
      enlargelimits=upper,
      clip=false,
      xtick={0,2,...,16},
      ytick={0,2,...,16}]
\addplot[red, domain=0:5,restrict y to domain=0:18, samples=100]  {pow(2,x)} node[right,anchor=north west]{$y=2^n$};
\addplot[blue, domain=0:18, samples=100]  {x} node[anchor=north east,inner xsep=3ex] {$y=n$};
\end{axis}
\end{tikzpicture}
\end{center}
\end{document}

You can dispense with pos by playing with anchors and inner sep as I did.

enter image description here

Related Question