Manual for Package pgfplots
2D/3D Plots in LATeX, Version 1.18.1
http://sourceforge.net/projects/pgfplots
The Reference
4.19Alignment Options
4.19.1Basic Alignment¶
Alignment works with two main methods: a coordinate where the axis shall be drawn and an “anchor” inside of the axis which shall be drawn at this particular coordinate. This methodology is common for each TikZ node – and an axis is nothing but a (special) TikZ node. The coordinate can be specified using the at key, while the anchor can be specified with the anchor key. In most cases, it is sufficient to provide only an anchor – unless one needs more than one axis in the same picture environment.
-
/pgfplots/at={coordinate expression} ¶
Assigns a position for the complete axis image. This option works similarly to the at option of \node[at={coordinate expression}], see the PGF/TikZ manual. The common syntax is at={(x,y)}.
The idea is to provide an coordinate expression where the axis will be placed. The axis’ anchor will be placed at coordinate expression.
-
/pgfplots/anchor={name} (initially south west) ¶
Chooses one of the different possible positions inside of an axis which is placed with at. The at key defines the position where to place the axis inside of the embedding picture, the anchor key defines which point of the axis shall be positioned by ‘at’. The initial configuration assumes at={(0,0)}. Thus, anchor=center will place the axis’ center at the logical picture position \((0,0)\). Similarly, anchor=south west will position the lower left corner of the axis at \((0,0)\).
For users who are familiar with TikZ: an axis is actually a very special node, so anchors work as in the PGF/TikZ manual.
Anchors are useful in conjunction with horizontal or vertical alignment of plots, see the examples below.
There are four sets of anchors available: anchors positioned on the axis bounding box, anchors on the outer bounding box and anchors which have one coordinate on the outer bounding box and the other one at a position of the axis rectangle. Finally, one can place anchors near the origin.
In more detail, we have anchors on the axis rectangle (the bounding box around the axis),64
Anchors on the outer bounding box,
There are anchors which have one coordinate on the outer bounding box, and one on the axis rectangle,
And finally, we have origin anchors which are especially useful when axis lines pass through the origin,
There is a fifth anchor which is not directly related to the axis: you can provide the anchor of a named inner node. Thus, you can define your own anchor, by writing \node (name) at (point coordinate) {}; as follows (using the baseline option described below):
What happens is that a node is placed at (-2,0.75). Note that the options [pin=...] are merely to show the \node (the pin style has been defined by the pgfplots manual). Since a name can also be assigned using name=node’s name and since any pgfplots description is also a \node, you can align your plot at selected axis descriptions:
The default value is anchor=south west. You can use anchors in conjunction with the TikZ baseline option and/or \begin{pgfinterruptboundingbox} to perform alignment.
Each of the anchors on the axis rectangle has an equivalent to a coordinate in the axis description cs described in Section 4.9.1. That means the first set of anchors actually lives on the tight bounding box around the axis (without any ticks or descriptions). The south west anchor will always be the lower left corner of this bounding box, even in case of a rotated or skewed coordinate system.65 Similar statements hold for the other anchors.
64 Versions prior to pgfplots v.1.3 did not use the bounding box of the axis, they used axis coordinates to orient these anchors. This has been fixed. If you really want to undo the bugfix, see compat/anchors.
65 Note that this is only true for versions since 1.3.
4.19.2Vertical Alignment with baseline¶
-
/tikz/baseline(no value) ¶
The baseline option should be provided as argument to a tikzpicture. It configures TikZ to shift the picture position \(y=0\) to the embedding text’s baseline:
Consequently, the baseline option allows to align different tikzpictures. An axis is, by default, placed with at={(0,0)}, and the anchor key specifies which part of the axis is placed at (0,0). Consequently, the baseline option, together with anchor, allows to align different axes with the embedding text.
The default axis anchor is south west, which means that the picture coordinate \((0,0)\) is the lower left corner of the axis. As a consequence, the TikZ option “baseline” allows vertical alignment of adjacent plots:
Note that it is also possible to write baseline=5cm in which case the image offset at \(y=\;\)5cm will be used as baseline.
The baseline key is related to \begin{minipage}[alignment] or \begin{tabular}[alignment]: the alignment tells LaTeX which part of the minipage or tabular shall be positioned on the baseline. Thus, baseline does the same for pictures (with more freedom for alignment).
4.19.3Horizontal Alignment¶
Horizontal alignment can be done in two ways:
-
1. Using separate tikzpicture environments which have reduced bounding boxes or
-
2. A single tikzpicture environment in which the complete alignment is done.
The first approach requires the use of reduced bounding boxes and is discussed in Section 4.20.1.
The second approach, a single tikzpicture environment, employs the at and anchor keys to align parts of the images. For example, if you place multiple axes into a single tikzpicture and use the ‘anchor’ option, you can control horizontal alignment:
Here, the second axis uses at={(main plot.below south west)} to be placed below the first one. Furthermore, it has yshift=-0.1cm in order to leave additional space, and it uses anchor=north west to place the upper left corner at the specified position. Instead of the at={} construction, we could also have used yshift with larger negative shift.
4.19.4Alignment In Array Form (Subplots)¶
Sometimes multiple alignment axes in array form are desired. pgfplots supports this task in several ways which are described in the following. There are basically three related, yet different, approaches:
-
1. Simply place \begin{tikzpicture}...\end{tikzpicture} into a LaTeX table. This is straightforward; you would do the very same thing with \includegraphics.
In addition to \includegraphics, the baseline feature allows simple yet effective vertical alignment. In addition, the trim left and trim right features allow simple yet effective horizontal alignment (see below).
-
2. Use a single picture which contains an array of axes, i.e. a pattern like
\begin{tikzpicture} \matrix{ multiple axes }; \end{tikzpicture}.
This allows considerably simpler alignment! Alas, it needs special handling for legend entries due to a weakness of \matrix. If you use the external library (which is recommended), it takes more time since the picture gets larger.
-
3. Use the groupplots library shipped with pgfplots. It is specialized on axes in array form with particular strength if the axes are closely related (for example if they share axis descriptions like xlabel or even tick labels). Note, however, that the other approaches are better when it comes to automatic handling of bounding boxes.
The groupplots library is discussed in all detail in Section 5.8. This section discusses the other two approaches.
Array Alignment using LaTeX Tables
The idea is simple: use a LaTeX table and provide one tikzpicture for every cell. You are probably familiar with this sort of alignment, perhaps together with \includegraphics. It works in the very same way for pgfplots. The approach is the simplest one since it doesn’t need special knowledge. Its disadvantage, however, is more difficulty to control positions inside of the image (like differently sized axis descriptions).
Is is strongly recommended to employ the baseline option for each cell picture, which simplifies vertical alignment considerably. If you want a simple solution to place separate axes in array form, and you prefer to use one tikzpicture for every axis, the probably most simple and most effective way to get horizontal alignment are the trim left and trim right features – or styles based on them:
The trim axis left feature can be used to exclude axis descriptions on the left from the bounding box, and the trim axis right can exclude axis descriptions on the right from the bounding box. Thus, alignment is done using the vertical axis lines. Since both keys effectively modify the bounding box, they are documented in Section 4.20.1 “Bounding Box Restrictions”. Here is just a small example for array alignment by means of tabular, baseline and the trim left/trim right features:
% Preamble:
\pgfplotsset{width=7cm,compat=1.18}
\pgfplotsset{
small,
title=Trimmed bounding boxes
}
\begin{center}
\begin{tabular}{rl}
\begin{tikzpicture}[baseline,trim axis left]
\begin{axis}
\addplot {x};
\end{axis}
\end{tikzpicture}
&
\begin{tikzpicture}[baseline,trim axis right]
\begin{axis}[
ylabel={$f(x)=x^2$},
yticklabel pos=upper,
ylabel style={font=\Huge},
]
\addplot {x^2};
\end{axis}
\end{tikzpicture}
\\
\begin{tikzpicture}[baseline,trim axis left]
\begin{axis}[xlabel=$x$,xlabel style={font=\Huge}]
\addplot {x^3};
\end{axis}
\end{tikzpicture}
&
\begin{tikzpicture}[baseline,trim axis right]
\begin{axis}[yticklabel pos=upper]
\addplot {x^4};
\end{axis}
\end{tikzpicture}
\\
\end{tabular}
\end{center}
The example has \(2 \times 2\) axes. The baseline feature controls the vertical alignment: the lower axis lines are always on the same height. The trim axis left key is a style which tells TikZ to trim everything which is left of the left axis line. Similarly, the trim axis right key does not include picture parts right of the right axis line. Together with \begin{center} and the yticklabel pos=upper key, we get correct horizontal and vertical alignment together with centering at the left and right axis lines (without descriptions).
A strong advantage is that this type of alignment requires almost no changes to your pictures. Thus, you can copy–paste existing images (TeX code) relatively simple.
Note that the approach is fully compatible with the image externalization library: each picture is exported separately, and the bounding box restrictions (and the baseline offset) are stored in separate .dpth files. The trim left/trim right approach for horizontal alignment is the only supported way for reduced bounding boxes and image externalization.
Array Alignment using TikZ Matrices
While it is possible to use (for example) tabular combined with the vertical and horizontal alignment methods discussed above, it might be better to use a TikZ matrix since it automatically handles the size of axis descriptions.
A TikZ matrix is some sort of “graphical” table. It knows everything about picture alignment and it has more flexibility than tabular when it comes to graphics. The idea is to pack the complete array into a single picture.
The complete documentation of a TikZ matrix is beyond the scope of this manual, please refer to the PGF/TikZ manual for details. But we provide an example here:
So, a matrix is a picture element inside of tikzpicture. Its cells are separated by ‘&’ as in tabular (or, if ‘&’ causes problems, with \pgfmatrixnextcell). Its rows are separated by ‘\\’. Each cell is aligned using the cells’ anchor. Since, by default, the anchor of an axis is placed at the lower left corner, the example above is completely aligned, without the need for any bounding box modifications – even the labels are aligned correctly. If another anchor shall be used, simply place
in front of the matrix. This will use the same configuration for every sub-plot.
Unfortunately, the array alignment with \matrix needs special attention with legends. A legend is also a \matrix and TikZ matrices can’t be nested. You will need to use the legend to name feature (or to assemble a legend by means of \label and \ref) to overcome this weakness (see Section 4.9.6 for details).
4.19.5Miscellaneous for Alignment¶
-
Predefined node current axis ¶
A node which refers to the current axis or the last typeset axis.
You can use this node in axis descriptions, for example to place axis labels or titles.
If you use current axis inside of axis descriptions, the “current axis” is not yet finished. That means you can’t use any outer anchor inside of axis descriptions.
It is also possible to use current axis in any drawing or plotting commands inside of an axis (but no outer anchor as these are not defined when drawing commands are processed). This usage is similar to the axis description cs.