Graphing an exponential function and its inverse on the same Cartesian plane

Here is a quick example;

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}
\begin{document}
\begin{tikzpicture}
\begin{axis}[grid=both,
          xmax=10,ymax=10,
          axis lines=middle,
          restrict y to domain=-7:12,
          enlargelimits]
\addplot[green]  {pow(2,x)} node[above]{$y=2^x$};
\addplot[blue,domain=1/2^6:10,samples=100]  {log2(x)} node[above left] {$y=\log_2(x)$};
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here


For any TikZ solution at least there is one solution in PSTricks. But the converse is not necessarily true.

\documentclass[pstricks,border=15pt,12pt]{standalone}
\usepackage{pst-plot}

\begin{document}
\pslegend[rt]
{
    \color{blue}\rule{5mm}{1mm} & \color{blue}$f(x)=2^x$ \\
    \color{red}\rule{5mm}{1mm} & \color{red}$f(x)=\log_2(x)$
}
\begin{psgraph}[algebraic]{->}(0,0)(-3,-3)(7,7){12cm}{!}
    \psplot[linecolor=blue]{-2.5}{2.5}{2^x}
    \psplot[linecolor=red]{2 -2.5 exp}{2 2.5 exp}{log(x)/log(2)}
    \psplot[linestyle=dashed]{-2}{6}{x}
\end{psgraph}

\end{document}

enter image description here


This is another approach with TikZ

enter image description here

\documentclass[border=5mm,tikz]{standalone}
\usetikzlibrary{calc}
\usepackage{amsmath}

\begin{document}
\begin{tikzpicture}[scale=0.8, samples=100]
\draw[->] (-3.8,0) -- (5,0) node[right] {$x$};
\draw[->] (0,-4) -- (0,5) node[left] {$y$};
\node at (-2.8,-3)[above] {\footnotesize\color{gray} $y=x$};
\draw[smooth, domain=-3.8:2.3, color=green, thick] 
    plot (\x,{2^(\x)}) node [right] {\footnotesize $f(x)=2^x$};
\fill [green] ($(0,1)$) circle (1.5pt) node at (0,1.2)[left] {\color{green}$(0,1)$};
\draw[smooth, domain = 0.06:5, color=blue, thick] plot (\x,{log2(\x)});
\fill [blue] ($(1,0)$) circle (1.5pt) node at (1.4,0)[below] {\color{blue}$(1,0)$};
\node at (4.6,2.4)[above] {\footnotesize\color{blue}$f^{-1}(x)=\log_2 x$};
\draw[smooth, dashed, domain=-2.5:4.5, color=gray] plot (\x,\x);
\end{tikzpicture}

\end{document}

Tags:

Tikz Pgf