1

How can I display units (like ms) instead of 10⁻³ when using PFGPlots?

Normally I would load data from a CSV file, scaling it from seconds to something like ms before plotting it is an extra step I would like to avoid.

Here is an example of a plot where it would be useful:

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

\begin{document}
    \begin{figure}\centering
        \begin{tikzpicture}
        \begin{axis}[
        title={$V_{{out}}$ versus $t$},
        xlabel={$t$},
        ylabel={$V_{{out}}$}]
        \addplot[color=red,mark=x] coordinates {
            (0.0001,-1.531)
            (0.0002,-1.6065)
            (0.0003,-1.7963)
            (0.0004,-2.5868)
            (0.0005,-4.0210)
            (0.0006, -4.531)
            (0.0007,-7.3352)
            (0.0008,-11.5088)
            (0.0009,-16.1195)
            (0.0010,-21.2523)
            (0.0011,-26.3903)
        };
        \end{axis}
        \end{tikzpicture}
    \end{figure}
\end{document}

Plot

Mark Omo
  • 277

1 Answers1

2

This is what the pgfplots library units is good for. Apart from loading this library, the important changes are the additional keys change x base,x SI prefix=milli,x unit=s,. (I also changed out to be come upright.)

\documentclass[]{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\usepgfplotslibrary{units}
\begin{document}
    \begin{figure}\centering
        \begin{tikzpicture}
        \begin{axis}[
        title={$V_\mathrm{out}$ versus $t$},
        xlabel={$t$},
        change x base,
        x SI prefix=milli,x unit=s,
        ylabel={$V_\mathrm{out}$},
        ]
        \addplot[color=red,mark=x] coordinates {
            (0.0001,-1.531)
            (0.0002,-1.6065)
            (0.0003,-1.7963)
            (0.0004,-2.5868)
            (0.0005,-4.0210)
            (0.0006, -4.531)
            (0.0007,-7.3352)
            (0.0008,-11.5088)
            (0.0009,-16.1195)
            (0.0010,-21.2523)
            (0.0011,-26.3903)
        };
        \end{axis}
        \end{tikzpicture}
    \end{figure}
\end{document}

enter image description here

  • Can it auto-detect the units and select the right prefix? – Mark Omo Jan 28 '20 at 23:37
  • @MarkOmo How would pgfplots know what units the data is in? And I believe it does not automatically change the scale factor, if that's what you mean. There are, however, posts on this site which do something of that sort, I believe. –  Jan 28 '20 at 23:41
  • 1
    Nice answer (+1)! One consideration (for OP): if time is given in milliseconds, it would make sense to record the voltage on the same way (in volts). – Zarko Jan 29 '20 at 00:01
  • 1
    @MarkOmo I think one could start from here to detect the scaling factor, and convert it to a unit prefix. Of course, I cannot exclude that this is already done somewhere but if it is not this will be a major surgery, I am afraid. (Of course I agree with Zarko's suggestion, too.) –  Jan 29 '20 at 00:24