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.13Ternary Diagrams

  • \usepgfplotslibrary{ternary} % and plain

  • \usepgfplotslibrary[ternary] % Cont

  • \usetikzlibrary{pgfplots.ternary} % and plain

  • \usetikzlibrary[pgfplots.ternary] % Cont

  • A library to draw ternary diagrams.

    A ternary diagram visualizes three-component systems such that the sum of them yields \(100\%\). Ternary diagrams are triangular axes.

5.13.1Ternary Axis
  • \begin{ternaryaxis}[(math image)options(math image)]

  • (math image)environment contents(math image)

  • \end{ternaryaxis}

  • The axis environment for ternary axes.

    (-tikz- diagram)

    % Preamble: \pgfplotsset{width=7cm,compat=1.18}\usepgfplotslibrary{ternary} \begin{tikzpicture} \begin{ternaryaxis} \addplot3 coordinates { (0.81, 0.19, 0.00) (0.76, 0.17, 0.07) (0.66, 0.16, 0.16) (0.76, 0.07, 0.17) (0.81, 0.00, 0.19) }; \addplot3 coordinates { (0.85, 0.15, 0.00) (0.82, 0.13, 0.05) (0.73, 0.14, 0.13) (0.82, 0.06, 0.13) (0.84, 0.00, 0.16) }; \legend{$10$\textdegree, $20$\textdegree} \end{ternaryaxis} \end{tikzpicture}

    A ternaryaxis works with relative coordinates: each data point consists of three components \(x\), \(y\), \(z\). Their sum forms a compound entity which has \(100\%\) (of whatever). In the standard configuration, we have \(x,y,z \in [0,1]\). The unit interval is not necessary: you can as well choose absolute data ranges \(x \in [x_{\min },x_{\max }]\), \(y \in [y_{\min },y_{\max }]\) and \(z \in [z_{\min },z_{\max }]\). The important thing is that the relative values

    \[ \tilde x := \frac {x-x_{\min }}{x_{\max } - x_{\min }},\; \tilde y := \frac {y-y_{\min }}{y_{\max } - y_{\min }},\; \tilde z := \frac {z-z_{\min }}{z_{\max } - z_{\min }} \]

    sum up to \(100\%\), i.e. \(\tilde x + \tilde y + \tilde z = 1\). Thus, pgfplots computes \(\tilde x\), \(\tilde y\) and \(\tilde z\) and interprets them as barycentric (triangular) coordinates.

    For this to work, it is crucial to provide xmin, xmax, ymin, ymax and zmin, zmax precisely! The initial configuration fixes them to the unit interval.

    What happens behind the scenes is that a data point \((x,y,z)\) is placed at \(X,Y\) determined by

    \[ \begin {bmatrix} X(x,y,z)\\ Y(x,y,z) \end {bmatrix} = \tilde x A + \tilde y B + \tilde z C = \begin {bmatrix} \frac 12 {\tilde x+2 \tilde z}\\ \frac {\sqrt 3}{2} {\tilde x} \end {bmatrix} \]

    where \(A=(\nicefrac 12,\nicefrac {\sqrt 3}{2})\) is top corner of the triangle, \(B=(0,0)\) the lower left and \(C=(1,0)\) the lower right one. The \(\tilde y\) component is not really necessary due to the linear dependency \(\tilde x+\tilde y+\tilde z=1\).

    The input coordinate \((100\%,0\%,0\%)\) is mapped to \(A\), the input coordinate \((0\%,100\%,0\%)\) to \(B\) and \((0\%,0\%,100\%)\) to \(C\) (Acrobat Reader: click into the axis to verify it).

    (-tikz- diagram)

    % Preamble: \pgfplotsset{width=7cm,compat=1.18}\usepgfplotslibrary{ternary} \begin{tikzpicture} \begin{ternaryaxis}[xlabel=A,ylabel=B,zlabel=C] \addplot3 coordinates { (0.81, 0.19, 0.00) (0.76, 0.17, 0.07) (0.66, 0.16, 0.16) (0.76, 0.07, 0.17) (0.81, 0.00, 0.19) }; \addplot3 coordinates { (0.85, 0.15, 0.00) (0.82, 0.13, 0.05) (0.73, 0.14, 0.13) (0.82, 0.06, 0.13) (0.84, 0.00, 0.16) }; \node [pin=130:Deduced $z$,draw=black] at (0.2,0.2) {}; \legend{$10$\textdegree, $20$\textdegree} \end{ternaryaxis} \end{tikzpicture}

A ternaryaxis can contain zero, one or more \addplot3 commands, just as a usual axis. In case you provide only two-dimensional coordinates (for example using \addplot or axis cs), the third component is deduced automatically such that components sum to \(100\%\). The \addplot3 command can use any of the accepted input formats, for example using coordinates, table, expression or whatever – but the input is always interpreted as barycentric coordinates (three components summing up to \(100\%\)).

(-tikz- diagram)

% Preamble: \pgfplotsset{width=7cm,compat=1.18}\usepgfplotslibrary{ternary} \begin{tikzpicture} \begin{ternaryaxis}[ title=Sloped labels and minor ticks, xlabel=Water, ylabel=D-Threonine, zlabel=L-Threonine, label style={sloped}, minor tick num=2, ] \addplot3 coordinates { (0.82, 0.18, 0.00) (0.75, 0.17, 0.08) (0.77, 0.12, 0.11) (0.75, 0.08, 0.17) (0.81, 0.00, 0.19) }; \addplot3 coordinates { (0.75, 0.25, 0.00) (0.69, 0.25, 0.06) (0.64, 0.24, 0.12) (0.655, 0.23, 0.115) (0.67, 0.17, 0.16) (0.66, 0.12, 0.22) (0.64, 0.11, 0.25) (0.69, 0.05, 0.26) (0.76, 0.01, 0.23) }; \end{ternaryaxis} \end{tikzpicture}

(-tikz- diagram)

% Preamble: \pgfplotsset{width=7cm,compat=1.18}\usepgfplotslibrary{ternary} \begin{tikzpicture} \begin{ternaryaxis}[ title=Sloped labels and minor grids, xlabel=Water, ylabel=D-Threonine, zlabel=L-Threonine, label style={sloped}, minor tick num=2, grid=both, ] \addplot3 coordinates { (0.82, 0.18, 0.00) (0.75, 0.17, 0.08) (0.77, 0.12, 0.11) (0.75, 0.08, 0.17) (0.81, 0.00, 0.19) }; \addplot3 coordinates { (0.75, 0.25, 0.00) (0.69, 0.25, 0.06) (0.64, 0.24, 0.12) (0.655, 0.23, 0.115) (0.67, 0.17, 0.16) (0.66, 0.12, 0.22) (0.64, 0.11, 0.25) (0.69, 0.05, 0.26) (0.76, 0.01, 0.23) }; \end{ternaryaxis} \end{tikzpicture}

A ternaryaxis supports (most of) the pgfplots axis interface, among them the grid option, the xtick={(math image)positions(math image)} way to provide ticks, including extra x ticks and its variants. Of course, it can also contain any of the mark, color and cycle list options of a normal axis.

The following example is a (crude) copy of an example of

http://www.sv.vt.edu/classes/MSE2094_NoteBook/96ClassProj/experimental/ternary2.html

and uses area style to change cycle list and the legend appearance.

(-tikz- diagram)

% Preamble: \pgfplotsset{width=7cm,compat=1.18}\usepgfplotslibrary{ternary} \begin{tikzpicture} \begin{ternaryaxis}[ title=Want--be--Stainless Steel, xlabel=Weight Percent Chromium, ylabel=Weight Percent Iron, zlabel=Weight Percent Nickel, label style=sloped, area style, ] \addplot3 table { A B C 1 0 0 0.5 0.4 0.1 0.45 0.52 0.03 0.36 0.6 0.04 0.1 0.9 0 }; \addlegendentry{Cr} \addplot3 table { A B C 1 0 0 0.5 0.4 0.1 0.28 0.35 0.37 0.4 0 0.6 }; \addlegendentry{Cr+$\gamma$FeNi} \addplot3 table { 0.4 0 0.6 0.28 0.35 0.37 0.25 0.6 0.15 0.1 0.9 0 0 1 0 0 0 1 }; \addlegendentry{$\gamma$FeNi} \addplot3 table { 0.1 0.9 0 0.36 0.6 0.04 0.25 0.6 0.15 }; \addlegendentry{Cr+$\gamma$FeNi} \addplot3 table { 0.5 0.4 0.1 0.45 0.52 0.03 0.36 0.6 0.04 0.25 0.6 0.15 0.28 0.35 0.37 }; \addlegendentry{$\sigma$+$\gamma$FeNi} \node [inner sep=0.5pt,circle,draw,fill=white,pin=-15:\footnotesize Stainless Steel] at (0.18,0.74,0.08) {}; \end{ternaryaxis} \end{tikzpicture}

Ternary plots can also use contour prepared to plot contour lines. The following example is a (crude) copy of an example of

http://www.sv.vt.edu/classes/MSE2094_NoteBook/96ClassProj/experimental/ternary2.html:

(-tikz- diagram)

% Preamble: \pgfplotsset{width=7cm,compat=1.18}\usepgfplotslibrary{ternary} \begin{tikzpicture} \begin{ternaryaxis}[ title=Want--be--Stainless Steel, xlabel=Weight Percent Chromium, ylabel=Weight Percent Iron, zlabel=Weight Percent Nickel, label style=sloped, ] % plotdata/pgfplotsternary.example1.dat: % % Chromium Iron Nickel Temperature % 0.90 0.0 0.10 1700 % 0.85 0.14 0.00 1700 % % 0.85 0.00 0.15 1600 % 0.78 0.22 0.00 1600 % 0.71 0.29 0.00 1600 % ... \addplot3 [contour prepared={labels over line}, point meta=\thisrow{Temperature}] table [x=Chromium,y=Iron,z=Nickel] {plotdata/pgfplotsternary.example1.dat}; \end{ternaryaxis} \end{tikzpicture}

The contour prepared={labels over line} installs the display style contour/labels over line and expects precomputed contour lines from the input stream. Here, the input stream is a table, consisting of the three relative components for Chromium, Iron and Nickel – and the point meta is set to be the Temperature column. The contour prepared style uses the \((x,y,z)\) coordinate to plot the data point and the point meta to determine contour labels (the initial configuration of contour prepared is to use point meta=z). The output thus allows to use both barycentric coordinates (ternary components) and contour labels.

5.13.2Tieline Plots
  • /tikz/tieline={(math image)options with tieline/ prefix(math image)}

  • \addplot+[tieline={(math image)options with tieline/ prefix(math image)}]

  • A plot handler for use in ternary diagrams which plots tie lines and binodal curves.

    On input, it accepts pairs of coordinates, \(A^{(i)}=(A_x^{(i)},A_y^{(i)},A_z^{(i)})\) and \(B^{(i)}=(B_x^{(1)},B_y^{(2)},B_z^{(3)})\), for \(i=1,\dotsc ,N\) (i.e. it requires a total of six coordinates, perhaps plus additional color data).

    On output, it connects the pairs, i.e. for every fixed \(i=1,\dotsc ,N\), it connects \(A^{(i)}\) – \(B^{(i)}\) (the so-called “tie lines”). In addition, it also draws the binodal curve, which is made up by connecting all \(A^{(i)}\) and then, in reverse ordering, all \(B^{(i)}\): \(A^{(1)}\) – \(A^{(2)}\) – \(\dotsb \) – \(A^{(N)}\) – \(B^{(N)}\) – \(B^{(N-1)}\) – \(\dotsb \) – \(B^{(1)}\):

    (-tikz- diagram)

    % Preamble: \pgfplotsset{width=7cm,compat=1.18}\usepgfplotslibrary{ternary} \begin{tikzpicture} \begin{ternaryaxis}[ xlabel=x (IPA), ylabel=y (water), zlabel=z (propene), axis on top, ] % plotdata/ternary_data.txt is a table of the form % A_propene A_water A_IPA B_propene B_water B_IPA % 0.0009 0.9990 0 0.9333 0.0667 0 % 0.0009 0.9988 0.0002 0.9303 0.0665 0.0032 % 0.0011 0.9975 0.0013 0.9135 0.0673 0.0191 % 0.0013 0.9962 0.0024 0.8956 0.0693 0.0351 % ... \addplot3 [tieline,fill=blue!10] table [x=A_IPA,y=A_water,z=A_propene] {plotdata/ternary_data.txt}; \end{ternaryaxis} \end{tikzpicture}

    We see that each input line has six columns, and each six columns are taken into account (this is different from other plot handlers!). The six columns make up the three components of the \(A\) and \(B\) points, respectively. In the example above, we used explicit column names and provided \(A_x\) using x=A_IPA, \(A_y\) using y=A_water and \(A_z\) using z=A_propene. Note that these keys are the common input method for \addplot table; they are nothing special (that means we could also use x index instead). The three columns for \(B\) can be provided manually (see below), or deduced automatically: in our case, the value for \(B_x\) has been found in the third column after x=A_IPA (which is B_IPA); the value for \(B_y\) has been found in the third column after y=A_water and \(B_z\) is made up from the third column after z=A_propene. In other words, the \(B\) value is searched (by default) by adding \(3\) to the column index of the respective \(A\) coordinate.

    You do not need to provide any column names; in this case, the first three columns make up \(A\) (in the order of appearance) and the following three make up \(B\).

    The only supported input type for tieline plots is table input. It is optimized to use \addplot3 table (as described above). To use the two-dimensional variant \addplot table, you need to tell pgfplots explicitly which columns make up \(A_x,A_y,B_x,B_y\); the \(z\)-coordinates are deduced automatically such that the result sums to \(100\%\).

    • /pgfplots/table/tie end x={(math image)colname(math image)} (initially empty)

    • /pgfplots/table/tie end y={(math image)colname(math image)} (initially empty)

    • /pgfplots/table/tie end z={(math image)colname(math image)} (initially empty)

    • /pgfplots/table/tie end x index={(math image)col index(math image)} (initially empty)

    • /pgfplots/table/tie end y index={(math image)col index(math image)} (initially empty)

    • /pgfplots/table/tie end z index={(math image)col index(math image)} (initially empty)

    • These keys can be used to provide column names or column indices for \(B_x\), \(B_y\) and \(B_z\), respectively. They can be provided like

      \addplot3[tieline] table[tie end y=B_water] ....

      Note that the tie end x keys are only available if the tieline option has been used before.

      The values for \(A\) are provided with table/x, table/x index and its variants as for any other plot type.

    The tieline plot handler accepts several options to customize the appearance. You can provide them as argument after tieline, using tieline={(math image)options(math image)}. In this case, the tieline/ prefix can be omitted. The keys are described in the following:

    • /pgfplots/tieline/each nth tie={(math image)number(math image)} (initially empty)

    • Allows to draw only each \(n\)th tie line, even though the binodal curve uses all provided coordinates:

      (-tikz- diagram)

      % Preamble: \pgfplotsset{width=7cm,compat=1.18}\usepgfplotslibrary{ternary} \begin{tikzpicture} \begin{ternaryaxis}[ xlabel=x (IPA), ylabel=y (water), zlabel=z (propene), axis on top, ] % plotdata/ternary_data.txt is a table of the form % A_propene A_water A_IPA B_propene B_water B_IPA % 0.0009 0.9990 0 0.9333 0.0667 0 % 0.0009 0.9988 0.0002 0.9303 0.0665 0.0032 % 0.0011 0.9975 0.0013 0.9135 0.0673 0.0191 % 0.0013 0.9962 0.0024 0.8956 0.0693 0.0351 % ... \addplot3 [ tieline={each nth tie=5}, fill=blue!10, ] table [x=A_IPA,y=A_water,z=A_propene] {plotdata/ternary_data.txt}; \end{ternaryaxis} \end{tikzpicture}

      Note that plot marks (if any) are drawn on every input position, use the mark repeat option to change that.

    • /pgfplots/tieline/tieline style={(math image)options(math image)}

    • Appends (math image)options(math image) to the style tieline/every tieline.

      Useful (math image)options(math image) are, for example, other plot handlers to adjust the appearance of tie lines. Suppose that you have additional color data for every tie line (which might have been provided as further input column). In our case, we provide random color data using point meta=rand, and visualize the single tielines as with contour prepared:

      (-tikz- diagram)

      % Preamble: \pgfplotsset{width=7cm,compat=1.18}\usepgfplotslibrary{ternary} \begin{tikzpicture} \begin{ternaryaxis}[ xlabel=x (IPA), ylabel=y (water), zlabel=z (propene), axis on top, ] % plotdata/ternary_data.txt is a table of the form % A_propene A_water A_IPA B_propene B_water B_IPA % 0.0009 0.9990 0 0.9333 0.0667 0 % 0.0009 0.9988 0.0002 0.9303 0.0665 0.0032 % 0.0011 0.9975 0.0013 0.9135 0.0673 0.0191 % 0.0013 0.9962 0.0024 0.8956 0.0693 0.0351 % ... \addplot3 [ point meta=rand, tieline={ each nth tie=8, tieline style={contour prepared}, }, fill=blue!10, ] table [x=A_IPA,y=A_water,z=A_propene] {plotdata/ternary_data.txt}; \end{ternaryaxis} \end{tikzpicture}

      The effect here is that contour labels and line colors are chosen for every tie line, where the actual color is determined using point meta and colormap. Other choices for plot handlers in tieline style might be the mesh.

    • /pgfplots/tieline/curve style={(math image)options(math image)}

    • Appends (math image)options(math image) to the style tieline/every curve.

      The curve style allows to customize the plot handler for the curve. A possible choice might be curve style={smooth} or a separate fill/draw color.