Modifying some features on the graphs of a function and its inverse

The following code is compiled by pgfplots to graph an exponential function and its inverse. I have some questions about the code, and I would like to modify some of the features of the graphs.

How is pgfplots instructed to draw grid lines 5 units apart? Why is the graph of the exponential function between the vertical lines x=-5 and x=3.8, I guess? Why is the graph of the line y=x between the vertical lines x=-5 and x=5? Why does the label for the logarithmic function interfere with the graph of the logarithmic function?

I would like the numbers on the axes to be in white nodes - so that the grid lines are not drawn over them. The grid lines are drawn too high and too far to the right. I would like either the grid lines to be drawn a bit shorter or the axes to be drawn a bit longer. The domain of the exponential function (that is graphed) should be from -6 to 4, and the domain of the linear function should be from -6 to 16.

\documentclass[10pt]{amsart}
\usepackage{tikz}
\usetikzlibrary{calc,angles,positioning,intersections,quotes,decorations.markings}
\usepackage{tkz-euclide}
\usetkzobj{all}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}

\begin{document}
\begin{tikzpicture}
\begin{axis}[grid=both,
xmax=18,ymax=17,
axis lines=middle,
restrict y to domain=-7:17,
enlargelimits]
\addplot[dashed]{x} node[fill=white, above, right]{$y=x$};
\addplot[green]  {pow(2,x)} node[fill=white, above, right]{$y=2^x$};
\addplot[blue,domain=1/2^6:16,samples=100]  {log2(x)} node[fill=white, below] {$y=\log_2(x)$};
\end{axis}
\end{tikzpicture}

\end{document}

• While I did answer your question, generally it is better to ask questions that are focussed on a specific issue. – Torbjørn T. Sep 14 '14 at 8:22

I guess the distance between ticks/grid lines is calculated with on some algorithm based on the domains and width/height of the axis, though I do not know this.

The default domain is -5:5, so unless otherwise specified (as you've done with the log2(x) plot) functions are plotted in that range. Why that range? Why not? Probably an arbitrary choice, that works fine in most cases. Only the developer, Christian Feuersänger, will know for sure I suppose.

The exponential function stops at around x=4 because of the restrict y to domain=-7:17 key, which removes all points where the y-values are above 17. The reason the line stops a bit earlier is that pgfplots does not, of course, draw infinitely many points. The default is to use 25 samples, and with 25 linearly spaced points between -5 and 5 you get one x-value at 3.75, and the next at 4.17. The latter gives a y-value of 17.36, and is therefore removed.

The label covers part of the line because of fill=white. The node shape is by default a rectangle, with some space from the text to the node edge. There is no automatic detection of conflicts with other lines. One way of fixing this is to shift the node down a bit, e.g. with below=5pt instead of just below.

Grid lines are not drawn above the ticks, but if you want a white fill, add ticklabel style={fill=white} to the axis options.

To extend the axes lines beyond the grid and add arrows at both ends you can add axis line style={shorten >=-0.5cm,shorten <=-0.5cm,latex-latex} to the axis options. Here, latex is the name of an arrowhead, you can use any of the arrowheads available in TikZ/PGF.

Adding the same to the \addplot options for the y=x line gives you arrowheads here are as well.

\documentclass[10pt]{amsart}
\usepackage{tikz}
\usetikzlibrary{calc,angles,positioning,intersections,quotes,decorations.markings}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}
\begin{document}
\begin{tikzpicture}
\begin{axis}[grid=both,
xmax=18,ymax=17,
axis lines=middle,
restrict y to domain=-7:17,
enlargelimits,
axis line style={shorten >=-0.5cm,shorten <=-0.5cm,latex-latex},
ticklabel style={fill=white}]
\addplot[dashed,domain=-6:16,latex-latex] {x} node[fill=white, above, right]{$y=x$};
\addplot[green,domain=-6:4]  {pow(2,x)} node[fill=white, above, right]{$y=2^x$};
\addplot[blue,domain=1/2^6:16,samples=100]  {log2(x)} node[fill=white, below=5pt] {$y=\log_2(x)$};
\end{axis}
\end{tikzpicture}

\end{document}


• Very nice. I forgot to mention that I want arrowhead at the left end of the x-axis and the bottom of the y-axis and at both ends of the line y=x. Sorry. – user143462 Sep 13 '14 at 22:30
• In the original code, I had the command restrict y to domain=-7:17. Pgfplots should have graphed the exponential function to the point (4.087, 17). The exponential function was below the horizontal line y=15, though. Why is the default domain -5:5? – user143462 Sep 13 '14 at 22:37
• @user143462 See my updated answer. – Torbjørn T. Sep 14 '14 at 7:53
• This is the graph that I wanted. Thanks. To clarify your comment on the reason that my original code stopped graphing the exponential function below the horizontal line y = 15, I will ask two questions about "sample points" for graphing. Are the first and last sample points -5 and 5 - so that the distance between sample points is 10/24 and sample point 22 is at 3.75 and sample point 23 is at about 4.1667? Could I have avoided this by having 101 sample points? – user143462 Sep 14 '14 at 13:25
• @user143462 Yes, the domain is closed so the first point is at -5 and the last at 5, and yes, increasing the number of sample points can fix the problem, as the distance between points will be reduced. – Torbjørn T. Sep 14 '14 at 15:16