5

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?

  • Use enlargelimits=false, or enlargelimits=upper. – user11232 Mar 26 '15 at 10:51
  • 2
    @HarishKumar: thanks for your comment. enlargelimits=upper works without having to set the position manually again. – jermenkoo Mar 26 '15 at 10:53
6

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

|improve this answer|||||
  • thanks a lot! The inner xsep and (newly discovered) inner ysep was particularly helpful with setting the position of function names. – jermenkoo Mar 26 '15 at 11:08

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.