tikz.dev / PGFplots Manual

Manual for Package pgfplots
2D/3D Plots in LA, Version 1.18.1
http://sourceforge.net/projects/pgfplots

Related Libraries

\(\newcommand{\footnotename}{footnote}\) \(\def \LWRfootnote {1}\) \(\newcommand {\footnote }[2][\LWRfootnote ]{{}^{\mathrm {#1}}}\) \(\newcommand {\footnotemark }[1][\LWRfootnote ]{{}^{\mathrm {#1}}}\) \(\let \LWRorighspace \hspace \) \(\renewcommand {\hspace }{\ifstar \LWRorighspace \LWRorighspace }\) \(\newcommand {\mathnormal }[1]{{#1}}\) \(\newcommand \ensuremath [1]{#1}\) \(\newcommand {\LWRframebox }[2][]{\fbox {#2}} \newcommand {\framebox }[1][]{\LWRframebox } \) \(\newcommand {\setlength }[2]{}\) \(\newcommand {\addtolength }[2]{}\) \(\newcommand {\setcounter }[2]{}\) \(\newcommand {\addtocounter }[2]{}\) \(\newcommand {\arabic }[1]{}\) \(\newcommand {\number }[1]{}\) \(\newcommand {\noalign }[1]{\text {#1}\notag \\}\) \(\newcommand {\cline }[1]{}\) \(\newcommand {\directlua }[1]{\text {(directlua)}}\) \(\newcommand {\luatexdirectlua }[1]{\text {(directlua)}}\) \(\newcommand {\protect }{}\) \(\def \LWRabsorbnumber #1 {}\) \(\def \LWRabsorbquotenumber "#1 {}\) \(\newcommand {\LWRabsorboption }[1][]{}\) \(\newcommand {\LWRabsorbtwooptions }[1][]{\LWRabsorboption }\) \(\def \mathchar {\ifnextchar "\LWRabsorbquotenumber \LWRabsorbnumber }\) \(\def \mathcode #1={\mathchar }\) \(\let \delcode \mathcode \) \(\let \delimiter \mathchar \) \(\def \oe {\unicode {x0153}}\) \(\def \OE {\unicode {x0152}}\) \(\def \ae {\unicode {x00E6}}\) \(\def \AE {\unicode {x00C6}}\) \(\def \aa {\unicode {x00E5}}\) \(\def \AA {\unicode {x00C5}}\) \(\def \o {\unicode {x00F8}}\) \(\def \O {\unicode {x00D8}}\) \(\def \l {\unicode {x0142}}\) \(\def \L {\unicode {x0141}}\) \(\def \ss {\unicode {x00DF}}\) \(\def \SS {\unicode {x1E9E}}\) \(\def \dag {\unicode {x2020}}\) \(\def \ddag {\unicode {x2021}}\) \(\def \P {\unicode {x00B6}}\) \(\def \copyright {\unicode {x00A9}}\) \(\def \pounds {\unicode {x00A3}}\) \(\let \LWRref \ref \) \(\renewcommand {\ref }{\ifstar \LWRref \LWRref }\) \( \newcommand {\multicolumn }[3]{#3}\) \(\require {textcomp}\) \( \newcommand {\meta }[1]{\langle \textit {#1}\rangle } \) \(\newcommand {\toprule }[1][]{\hline }\) \(\let \midrule \toprule \) \(\let \bottomrule \toprule \) \(\def \LWRbooktabscmidruleparen (#1)#2{}\) \(\newcommand {\LWRbooktabscmidrulenoparen }[1]{}\) \(\newcommand {\cmidrule }[1][]{\ifnextchar (\LWRbooktabscmidruleparen \LWRbooktabscmidrulenoparen }\) \(\newcommand {\morecmidrules }{}\) \(\newcommand {\specialrule }[3]{\hline }\) \(\newcommand {\addlinespace }[1][]{}\) \(\require {colortbl}\) \(\let \LWRorigcolumncolor \columncolor \) \(\renewcommand {\columncolor }[2][named]{\LWRorigcolumncolor [#1]{#2}\LWRabsorbtwooptions }\) \(\let \LWRorigrowcolor \rowcolor \) \(\renewcommand {\rowcolor }[2][named]{\LWRorigrowcolor [#1]{#2}\LWRabsorbtwooptions }\) \(\let \LWRorigcellcolor \cellcolor \) \(\renewcommand {\cellcolor }[2][named]{\LWRorigcellcolor [#1]{#2}\LWRabsorbtwooptions }\) \(\newcommand {\intertext }[1]{\text {#1}\notag \\}\) \(\let \Hat \hat \) \(\let \Check \check \) \(\let \Tilde \tilde \) \(\let \Acute \acute \) \(\let \Grave \grave \) \(\let \Dot \dot \) \(\let \Ddot \ddot \) \(\let \Breve \breve \) \(\let \Bar \bar \) \(\let \Vec \vec \) \(\newcommand {\nicefrac }[3][]{\mathinner {{}^{#2}\!/\!_{#3}}}\)

5.14Units in Labels

by Nick Papior Andersen

  • \usepgfplotslibrary{units} % and plain

  • \usepgfplotslibrary[units] % Cont

  • \usetikzlibrary{pgfplots.units} % and plain

  • \usetikzlibrary[pgfplots.units] % Cont

  • A library which allows to use automatic typesetting of units in labels. The library utilizes different keys to typeset the final output in a consistent way. Calling one of the commands automatically sets the key ‘use units=true’ so one does not have to worry about this.

pgfplots has the capability of supporting units. This provides quick customization of the plot as well as the addition of units in labels.

Loading the library automatically enables the typesetting of units in labels. Currently it only supports predefined SI units but a per-user customization is also implemented such that it can be used in any way you like.

First the key which enables you to switch on/off the unit system.

  • /pgfplots/use units={(math image)boolean(math image)} (initially true)

  • This key simply enables pgfplots to use what is described next. This key will be set to true if you load the library. You can use this to temporarily determine whether the unit library should be used in plots.

  • /pgfplots/x unit={(math image)unit(math image)} (initially empty)

  • /pgfplots/y unit={(math image)unit(math image)} (initially empty)

  • /pgfplots/z unit={(math image)unit(math image)} (initially empty)

  • These keys set the unit in their respective axis. In SI units you could for instance set the x unit in Newton as x unit=N.

  • /pgfplots/x unit prefix={(math image)prefix(math image)} (initially empty)

  • /pgfplots/y unit prefix={(math image)prefix(math image)} (initially empty)

  • /pgfplots/z unit prefix={(math image)prefix(math image)} (initially empty)

  • These keys set the prefix of the unit. If a value on the y axis is in kilo you would set the y unit prefix=k. Prefix will be typeset in front of the unit.

    This command will not intervene with the basis of the axis system. That is a prefix as just mentioned will not divide every y axis number by 1000. In order to do this, see key (math image)axis(math image) SI prefix, see Section 5.14.1.

    Notice that if the (math image)axis(math image) unit isn’t set the entire unit will not be typeset.

Remarks:

Remember that all typesetting of labels occur within math mode (i.e. within $ delimiters). Therefore one can use \frac and other mathematics commands.

Often one just has to utilize the above mentioned keys. It is the basis of the unit typesetting system provided by pgfplots.

(-tikz- diagram)

% Preamble: \pgfplotsset{width=7cm,compat=1.18}\usepgfplotslibrary{units} \begin{tikzpicture} \begin{axis}[use units, x unit=m,x unit prefix=k, y unit=N,y unit prefix=m, xlabel=Distance,ylabel=Force, ] \addplot coordinates { (1,2.3) (2,2.7) (3,2.1) (4,1.8) (5,1.5) (6,1.1) }; \end{axis} \end{tikzpicture}

Below is an example of what would be obtained according to the styles

% x label becomes ``Temperature [T]'', y label becomes ``Nothing'' \pgfplotsset{use units,x unit=T,xlabel=Temperature,ylabel=Nothing} % x label becomes ``Temperature'', y label becomes ``Nothing'' \pgfplotsset{use units,x unit prefix=m,xlabel=Temperature,ylabel=Nothing}

Notice the second example. Only setting the prefix will not activate the unit typesetting. Therefore one should ensure to use the x unit key if the typesetting of the labels should be done.

For typesetting the units one can also change the appearance. For instance one might not like the square brackets which surround the unit. These can luckily be changed using the below keys.

  • /pgfplots/unit marking pre={(math image)pre(math image)} (initially \left[)

  • /pgfplots/unit marking post={(math image)post(math image)} (initially \right])

  • /pgfplots/unit markings=parenthesis|square brackets|slash space (initially square brackets)

  • These keys set the surroundings of the unit. The initial yields \(\left [\frac {1}{2}\right ]\) such that you can typeset fractions in units. Be aware that you can only obtain large fractions if you use \dfrac. These can easily be set using the option key unit markings where the options typesets as the following

    \pgfplotsset{x unit=T,unit markings=parenthesis} % x unit becomes `` \left(T\right)'' \pgfplotsset{x unit=T,unit markings=square brackets} % x unit becomes `` \left[T\right]'' \pgfplotsset{x unit=T,unit markings=slash space} % x unit becomes `` / T''

    Notice that all typesetting of units first inserts a space and then the unit marking pre code.

    Of course you can just manually set each of them with the unit marking pre and unit marking post keys. Just remember that they are typeset within a $ delimiters.

One will typically typeset the unit with a specific font. To do so an option of changing the typesetting command is supplied.

  • /pgfplots/unit code/.code 2 args={(math image)...(math image)}

  • This can be utilized to great extent. By default, units are typeset as \mathrm{(math image)unit prefix(math image)(math image)unit(math image)}. But if one for instance wishes to utilize the package siunitx, which has great capabilities in typesetting both units, numbers and angles, one can just set the key as

    \pgfplotsset{unit code/.code 2 args={\si{#1#2}}}

    which would yield the unit as \si{(math image)unit prefix(math image)(math image)unit(math image)}.

    The first argument is typeset as (math image)unit prefix(math image) and the second argument is (math image)unit(math image).

    The most important thing is that the command needs exactly two arguments. So if you would like a command that typesets the prefix in bold face and the unit in normal roman font you should call

    \pgfplotsset{unit code/.code 2 args={\mathbf{#1}\mathrm{#2}}
5.14.1Preset SI prefixes

To support the SI system a number of preset keys are defined. This should yield a more intuitive way of supplying the prefix as well as add some more functionality. For instance it provides an easy scaling mechanism.

  • /pgfplots/x SI prefix=yocto||milli|centi|deci|deca|hecto|kilo||yotta (initially none)

  • /pgfplots/y SI prefix=yocto||milli|centi|deci|deca|hecto|kilo||yotta (initially none)

  • /pgfplots/z SI prefix=yocto||milli|centi|deci|deca|hecto|kilo||yotta (initially none)

  • /pgfplots/change x base=true|false (initially false)

  • /pgfplots/change y base=true|false (initially false)

  • /pgfplots/change z base=true|false (initially false)

  • These keys sets the prefix of the unit. The allowed prefixes are:

    .
    Prefix Power
    yocto -24
    zepto -21
    atto -18
    femto -15
    pico -12
    nano -9
    micro -6
    milli -3
    centi -2
    deci -1

        

    .
    Prefix Power
    deca 1
    hecto 2
    kilo 3
    mega 6
    giga 9
    tera 12
    peta 15
    exa 18
    zetta 21
    yotta 24

    As well as resetting the base of the axis if the key change (math image)axis(math image) base=true. Just remember to set the change (math image)axis(math image) base before using the (math image)axis(math image) SI prefix key.

    See the utilization as in the example below.

    (-tikz- diagram)

    % Preamble: \pgfplotsset{width=7cm,compat=1.18}\usepgfplotslibrary{units} \begin{tikzpicture} \begin{axis}[ change x base, x SI prefix=kilo,x unit=m, y SI prefix=milli,y unit=N, xlabel=Distance,ylabel=Force, ] \addplot coordinates { (1000,1) (2000,1.1) (3000,1.2) (4000,1.3) }; \end{axis} \end{tikzpicture}

    Notice that the x axis has changed base without displaying the \(\cdot 10^{3}\). This is done by using the key change x base. Even though you have used the key y SI prefix=milli the base isn’t changed on the y axis. Try adding change y base just after change x base and see the result!

The above keys are the easy implementation of the base change. Below is a further customization of the base change. It makes it easy to implement a prefix with a custom base change.