[Tex/LaTex] Plot a function of a binary logarithm with TikZ

pgfplotsplottikz-pgf

How do I plot a function of a binary logarithm with TikZ? Say that I want to plot f(x) = log_2 x.

Best Answer

The pgfmath function that you can use directly in TikZ is called log2. PGFplots uses the fpu library, which doesn't have this function, but you can get the same result by using ln(x)/ln(2) instead:

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture}[scale=0.7]
\draw [help lines] (0,-4) grid [step=1] (10,4);
\draw (0,0) -- (10,0);
\draw plot [domain=0.1:10,samples=100] (\x,{log2(\x)});
\draw plot [domain=0.1:10,samples=100] (\x,{log2(\x)});
\end{tikzpicture}

\vspace{1cm}

\begin{tikzpicture}[trim axis left]
\begin{axis}[domain=0:10,
  samples=100,
  enlarge x limits=false,
  grid=both,
  no markers,
  axis equal]
\addplot +[thick] {ln(x)/ln(2)};
\end{axis}
\end{tikzpicture}
\end{document}

log2

Related Question