tikz.dev / PGFplots Manual

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

Utilities and Basic Level Commands

\(\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}}}\)

9.1Utility Commands

  • \foreach(math image)variables(math image) in (math image)list(math image) {(math image)commands(math image)}

  • A powerful loop command provided by TikZ, see the TikZ manual for details.

    Iterating 1. Iterating 2. Iterating 3. Iterating 4.

    \foreach \x in {1,2,...,4} {Iterating \x. }%

    A pgfplots related example could be

    \foreach \i in {1,2,...,10} {\addplot table {datafile\i}; }%
  • \pgfplotsforeachungrouped(math image)variable(math image) in (math image)list(math image) {(math image)command(math image)}

  • A specialised variant of \foreach which can do two things: it does not introduce extra groups while executing (math image)command(math image) and it allows to invoke the math parser for (simple!) (math image)\(x_0\)(math image),(math image)\(x_1\)(math image),...,(math image)\(x_n\)(math image) expressions.

    Iterating 1. Iterating 2. Iterating 3. Iterating 4. All collected = , 1, 2, 3, 4.

    \def\allcollected{} \pgfplotsforeachungrouped \x in {1,2,...,4} {Iterating \x. \edef\allcollected{\allcollected, \x}}% All collected = \allcollected.

    A more useful example might be to work with tables. The following example is taken from PgfplotsTable:

    \pgfplotsforeachungrouped \i in {1,2,...,10} {% \pgfplotstablevertcat{\output}{datafile\i} % appends `datafile\i' -> `\output' }% % since it was ungrouped, \output is still defined (would not work % with \foreach)

Remark:

The special syntax (math image)list(math image)=(math image)\(x_0\)(math image),(math image)\(x_1\)(math image),...,(math image)\(x_n\)(math image), i.e. with two leading elements, followed by dots and a final element, invokes the math parser for the loop. Thus, it allows larger number ranges than any other syntax if /pgf/fpu is active. In all other cases, \pgfplotsforeachungrouped invokes \foreach and provides the results without groups.

Keep in mind that inside of an axis environment, all loop constructions (including custom loops, \foreach and \pgfplotsforeachungrouped) need to be handled with care: loop arguments can only be used in places where they are immediately evaluated; but pgfplots postpones the evaluation of many macros. For example, to loop over something and to generate axis descriptions of the form \node at (axis cs:\i,0.5)..., the loop macro \i will be evaluated in \end{axis} – but at that time, the loop is over and its value is lost. The correct way to handle such an application is to expand the loop variable explicitly. For example:

\pgfplotsforeachungrouped \i/\j in { 1 / a, 2 / b, 3 / c }{ \edef\temp{\noexpand\node at (axis cs: \i,0.5) {\j};} % \show\temp % lets TeX show you what \temp contains \temp }

The example generates three loop iterations: \i=1, \j=a; then \i=2, j=b; then \i=3, \j=c. Inside of the loop body, it expands them and assigns the result to a macro using an “expanded definition”, \edef. The result no longer contains either \i or \j (since these have been expanded). Then, it invokes the resulting macro. Details about the command \edef and expansion control can be found in the document TeX-programming-notes.pdf which comes with pgfplots.

Restrictions:

you can’t nest this command yet (since it does not introduce protection by scopes).

  • \pgfmathparse{(math image)expression(math image)}

  • Invokes the pgf math parser for (math image)expression(math image) and defines \pgfmathresult to be the result.

    The result is ‘42.0’.

    \pgfmathparse{1+41} The result is `\pgfmathresult'.

    The math engine in pgf typically uses ’s internal arithmetics. That means: it is well suited for numbers in the range \([-16384,16384]\) and has a precision of \(5\) digits.

    The number range is typically too small for plotting applications. pgfplots improves the number range by means of \pgfkeys{/pgf/fpu}\pgfmathparse{1+41} to activate the “floating point unit” (fpu) and to apply all following operations in floating point.

    In pgfplots, the key /pgfplots/use fpu is typically on, which means that any coordinate arithmetics are carried out with the fpu. However, all pgf related drawing operations still use the standard math engine.

    In case you ever need to process numbers of extended precision, you may want to use

    The result is ‘1 · 106 ’.

    \pgfkeys{/pgf/fpu}% \pgfmathparse{1000*1000} The result is `\pgfmathprintnumber{\pgfmathresult}'.

    Note that results of the fpu are typically not in human-readable format, so \pgfmathprintnumber is the preferred way to typeset such numbers.

    Please refer to the PGF/TikZ manual for more details.

  • /pgfplots/use fpu=true|false (initially true)

  • pgfplots comes with different approaches to compute math expressions and use fpu is the most powerful. It implements math operations either in the lua backend or in a pure implementation and comes with a high number range and adequate precision.

    However, the values stored in \pgfmathresult are cryptic and need to be processed by means of special macros. The switch use fpu is only useful if this number format results in difficulties, i.e. it is a debug switch which should never be used in normal operations.

  • /pgf/declare function=(math image)function definitions(math image)

  • Allows to define one or more functions.

    The argument (math image)function definitions(math image) can contain one or more definitions, and each must be terminated by a semicolon:

    (-tikz- diagram)

    % Preamble: \pgfplotsset{width=7cm,compat=1.18} \begin{tikzpicture} \begin{axis}[ declare function={ C=4; square(\t)=(\t)^2 + C; }, ] \addplot+ [samples=2] {C*x}; \addplot {square(x)}; \end{axis} \end{tikzpicture}

    The definitions as such have the form (math image)function(math image)(math image)argument list(math image) = (math image)definition(math image) where the (math image)argument list(math image) contains a comma-separated-list of arguments like \t or \t,\a,\b. The (math image)definition(math image) is a math expression which makes use of these arguments.

    Please refer to the PGF/TikZ manual for more details.

  • \pgfplotstableread{(math image)file(math image)}

  • Please refer to the manual of PgfplotsTable, pgfplotstable.pdf, which is part of the pgfplots bundle.

  • \pgfplotstabletypeset{(math image)\macro(math image)}

  • Please refer to the manual of PgfplotsTable, pgfplotstable.pdf, which is part of the pgfplots bundle.

  • \pgfplotsiffileexists{(math image)filename(math image)}{(math image)true code(math image)}{(math image)false code(math image)}

  • Invokes (math image)true code(math image) if (math image)filename(math image) exists and (math image)false code(math image) if not. Can be used in looping macros, for example to plot every data file until there are no more of them.

  • \pgfplotsutilifstringequal{(math image)first(math image)}{(math image)second(math image)}{(math image)true code(math image)}{(math image)false code(math image)}

  • A simple “strcmp” tool which invokes (math image)true code(math image) if (math image)first(math image) \(=\)(math image)second(math image) and (math image)false code(math image) otherwise. This does not expand macros.

  • \pgfkeys

  • \pgfeov

  • \pgfkeysvalueof

  • \pgfkeysgetvalue

  • These commands are part of the TikZ way of specifying options, its sub-package pgfkeys. The \pgfplotsset command is actually nothing but a wrapper around \pgfkeys.

    A short introduction into \pgfkeys can be found in [9] whereas the complete reference is, of course, the PGF/TikZ manual.

    The key \pgfkeysvalueof{(math image)key name(math image)} expands to the value of a key; \pgfkeysgetvalue{(math image)key name(math image)}{(math image)\macro(math image)} stores the value of (math image)key name(math image) into (math image)\macro(math image). The \pgfeov macro is used to delimit arguments for code keys in \pgfkeys, please refer to the references mentioned above.