tikz.dev / PGFplots Manual

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

The Reference

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

4.4About Options: Preliminaries

pgfplots knows a whole lot of key–value options which can be (re)defined to activate desired features or modified to apply some fine-tuning.

A key usually has a value (like a number, a string, or perhaps some macro code). You can assign values to keys (“set keys”) in many places in a document. The value will remain effective until it is changed or until the current scope ends (which happens after a closing curly brace ‘}’, after \end{(math image)name(math image)} or, for example, after \addplot).

Most keys can be used like

\begin{tikzpicture} \begin{axis}[key=value,key2=value2] % axis-wide keys ... \end{axis} \end{tikzpicture}

which changes them for the complete axis. A key in this context can be any option defined in this manual, no matter if it has the /pgfplots/ or the /tikz/ key prefix. Note that key prefixes can be omitted in almost all cases.

A value can usually be provided without curly braces. For example, if the manual contains something like ‘xmin={(math image)\(x\)-coordinate(math image)}’, you can safely skip the curly braces. The curly braces are mandatory if values contain something which would otherwise confuse the key setup (for example an equal sign ‘=’ or a comma ‘,’).

Some keys can be changed individually for each plot:

\begin{tikzpicture} \begin{axis} % keys valid for single plots: \addplot ... ; % uses the "cycle list" to determine keys \addplot [key=value,key2=value2] ... ; % uses the provided keys (not the "cycle list") \addplot+ [key=value,key2=value2] ... ; % appends something to the "cycle list" \end{axis} \end{tikzpicture}

Besides these two possibilities, it is also possible to work with document-wide keys:

\chapter{My Chapter} \pgfplotsset{ key=value, key2=value2, } This chapter has a common key configuration: \begin{tikzpicture} \begin{axis}% uses the key config from above ... \end{axis} \end{tikzpicture}

In the example above, the \pgfplotsset command changes keys. The changes are permanent and will be used until

  • you redefine them or

  • the current environment (like \end{figure}) is ended or

  • encounters a closing brace ‘}’.

This includes document-wide preamble configurations like

\documentclass{article} \usepackage{pgfplots} \pgfplotsset{ xticklabel={$\mathsf{\pgfmathprintnumber{\tick}}$}, every axis/.append style={ font=\sffamily, }, } ...

The basic engine to manage key–value pairs is pgfkeys which is part of pgf. This engine always has a key name and a key “path”, which is somehow similar to file name and directory of files. The common “directory” (key path) of pgfplots is ‘/pgfplots/’. Although the key definitions below provide this full path, it is always (well, almost always) safe to skip this prefix – pgfplots uses it automatically. The same holds for the prefixes ‘/tikz/’ which are common for all TikZ drawing options and ‘/pgf/’ which are for the (more or less) low-level commands of pgf. All these prefixes can be omitted.

One important concept is the concept of styles. A style is a key which contains one or more other keys. It can be redefined or modified until it is actually used by the internal routines. Each single component of TikZ and pgfplots can be configured with styles.

For example,

sets the line width for every legend to 1pt by appending ‘line width=1pt’ to the existing style for legends.

There are keys like legend style, ticklabel style, and label style which allow to modify the predefined styles (in this case the styles for legends, ticklabels and axis labels, respectively). They are, in general, equivalent to a (math image)style name(math image)/.append style={} command (the only difference is that the /.append style thing is a little bit longer). There is also the possibility to define a new style (or to overwrite an already existing one) using /.style={}.

There are several other styles predefined to modify the appearance, see Section 4.18.

  • \pgfplotsset{(math image)key-value-list(math image)}

  • Defines or sets all options in (math image)key-value-list(math image). The (math image)key-value-list(math image) can contain any of the options in this manual which have the prefix /pgfplots/ (however, you do not need to type that prefix).

    Inside of (math image)key-value-list(math image), the prefixes ‘/pgfplots/’ which are commonly presented in this manual can be omitted (they are checked automatically).

    This command can be used to define default options for the complete document or a part of the document. For example,

    \pgfplotsset{ cycle list={ {red, mark=*}, {blue,mark=*}, {red, mark=x}, {blue,mark=x}, {red, mark=square*}, {blue,mark=square*}, {red, mark=triangle*}, {blue,mark=triangle*}, {red, mark=diamond*}, {blue,mark=diamond*}, {red, mark=pentagon*}, {blue,mark=pentagon*} }, legend style={ at={(0.5,-0.2)}, anchor=north, legend columns=2, cells={anchor=west}, font=\footnotesize, rounded corners=2pt, }, xlabel=$x$, ylabel=$f(x)$, }

    can be used to set document-wise styles for line specifications, the legends’ style and axis labels. The settings remain in effect until the end of the current environment (like \end{figure}) or until you redefine them or until the next closing curly brace ‘}’ (whatever comes first).

    You can also define new styles (collections of key–value pairs) with /.style and /.append style.

    \pgfplotsset{ My Style 1/.style={xlabel=$x$, legend entries={1,2,3} }, My Style 2/.style={xlabel=$X$, legend entries={4,5,6} } }

    The /.style and /.append style key handlers are described in Section 4.18 in more detail.

  • Key handler (math image)key(math image)/.code={(math image) code(math image)}

  • Occasionally, the pgfplots user interface offers to replace parts of its routines. This is accomplished using so called “code keys”. What it means is to replace the original key and its behavior with new (math image) code(math image). Inside of (math image) code(math image), any command can be used. Furthermore, the #1 pattern will be the argument provided to the key.

    I’ve been invoked with ‘this here’

    \pgfplotsset{ My Code/.code={I've been invoked with `#1'}} \pgfplotsset{My Code={this here}}

    The example defines a (new) key named My Code. Essentially, it is nothing else but a \newcommand, plugged into the key–value interface. The second statement “invokes” the code key.

  • Key handler (math image)key(math image)/.code 2 args={(math image) code(math image)}

  • As /.code, but this handler defines a key which accepts two arguments. When the so defined key is used, the two arguments are available as #1 and #2.

  • Key handler (math image)key(math image)/.cd

  • Each key has a fully qualified name with a (long) prefix, like /pgfplots/xmin. However, if the “current directory” is /pgfplots, it suffices to write just xmin. The /.cd key handler changes the “current directory” in this way.

    The prefixes /tikz/ and /pgfplots/ are checked automatically for any argument provided to \begin{axis}[(math image)options(math image)] or \addplot. So, you won’t need to worry about them, just omit them – and look closer in case the package doesn’t identify the option.

4.4.1PGFPlots and TikZ Options

This section is more or less technical and can be skipped unless one really wants to know more about this topic.

TikZ options and pgfplots options can be mixed inside of the axis arguments and in any of the associated styles. For example,

assigns the ‘legend columns’ option (a pgfplots option) and uses ‘font’ for drawing the legend (a TikZ option). The point is: legend columns needs to be known before the legend is typeset whereas font needs to be active when the legend is typeset. pgfplots sorts out any key dependencies automatically:

The axis environments will process any known pgfplots options, and all ‘every’ styles will be parsed for pgfplots options. Every unknown option is assumed to be a TikZ option and will be forwarded to the associated TikZ drawing commands. For example, the ‘font=\Large’ above will be used as argument to the legend matrix, and the ‘font=\Large’ argument in

will be used in the nodes for axis labels (but not the axis title, for example).

It is an error if you assign incompatible options to axis labels, for example ‘xmin’ and ‘xmax’ can’t be set inside of ‘every axis label’.