Sebastian's Blog

Writing About Things - Sometimes Even Interesting

PGFplots: Rescaling Axis and Add Units

Add the following to the LaTeX preamble to load the required packages and extensions:

% ...
\usepackage{tikz}
\usepackage{pgfplots}
\usetikzlibrary{units}

\begin{document}

% ...

With this, we can now do the following to label the x-axis as “Time in (s)” and the y-axis as “Power in (mW)”. At the same time this will ensure that all the y-values are scaled to match the SI-prefix. Neat!

\begin{tikzpicture}
  \begin{axis}
  % ...
  xlabel = {Time},
  ylabel = {Power},
  unit marking pre={\text{in }}, % add units as "<<axis label>> in ⟨unit⟩"
  unit marking post={}, % nothing comes after the unit
  change x base, % allow rescaling of x-axis (comment out if unneeded)
  change y base, % allow rescaling of y-axis (comment out if unneeded)
  x unit=s, % unit of data on x-axis
  y SI prefix=milli, % resacling factor of axis if neeeded
  y unit=W % unit of data on y-axis
  \end{axis}
  \addplot[...]{...}
\end{tikzpicture}