# Align axis of separate plots in pgfplots

I am preparing a talk using plots generated in pgfplots' axis-environment. I want the plots' axes to be of the exact same size and position. Unfortunately, plots with different axis- and plot-labels differ in size and produce small "jumps" when flipping through the pages of the presentation. In the code below, I illustrate the problem. The axes have the same size, but due to the different labels, their horizontal position shifts. If one comments the varwidthoption, one can see, that the same thing happens in the vertical direction.

\documentclass[crop,10pt,
varwidth=250pt
]{standalone}
\usepackage[english]{babel}
\usepackage{pgfplots}
\pgfplotsset{compat=1.9}
\begin{document}

\begin{tikzpicture}
\begin{axis}[
width=200pt,
heigth=200pt,
xmin=0,xmax=1,ymin=0,ymax=1,
xlabel=xlabel,
ylabel=ylabel,
scale only axis
]
(0.1,0.1)
(0.9,0.9)
};
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}
\begin{axis}[
width=200pt,
heigth=200pt,
xmin=0,xmax=100,ymin=0,ymax=100,
xlabel=x$_l^a$bel,
ylabel=y$_l^a$bel,
scale only axis,
]
(10,10)
(90,90)
};
\end{axis}
\end{tikzpicture}

\end{document}

Is there a way to produce separate plots with axes of exact same size and position?

• you can specify the width or height of the plots, you'll then probably also want to use scale only axis=true to apply this width and height to the rectangle. For more information see the pgfplots manual (section 4.10 for pgfplots version 1.10). – hugovdberg Mar 31 '14 at 12:57
• You may also need xmin, xmax, ymin and ymax. Please add your (minimal working) example to the post. It helps solvers to get into it. – Malipivo Mar 31 '14 at 13:07
• @hugovdberg @Malipivo I've added the mwe. I always use width and hight as well as the x/y min/max options, the problem comes from the different labels which lead to a different overall size of the graphics and a different position of the axes. – Janek Mar 31 '14 at 13:15

I think section 4.20 The Picture’s Size: Bounding Box and Clipping of the pgfplots manual (v1.10) might be of use, you can use the overlay style specification to exclude parts from the bounding box, thus aligning only the axes themselves, regardless of the size of the labels.

\documentclass{article}
\usepackage[english]{babel}
\usepackage{pgfplots}
\pgfplotsset{compat=1.9}
\begin{document}
\begin{center}
\begin{tikzpicture}
\begin{axis}[
width=200pt,
height=200pt,
xmin=0,xmax=1,ymin=0,ymax=1,
xlabel=xlabel,
ylabel=ylabel,
ylabel style={overlay},
yticklabel style={overlay},
xlabel style={overlay},
xticklabel style={overlay},
scale only axis
]
(0.1,0.1)
(0.9,0.9)
};
\end{axis}
\end{tikzpicture}
\end{center}
\vspace*{4em}
\begin{center}
\begin{tikzpicture}
\begin{axis}[
width=200pt,
height=200pt,
xmin=0,xmax=100,ymin=0,ymax=100,
xlabel=x$_l^a$bel,
ylabel=y$_l^a$bel,
ylabel style={overlay},
yticklabel style={overlay},
xlabel style={overlay},
xticklabel style={overlay},
scale only axis,
]
(10,10)
(90,90)
};
\end{axis}
\end{tikzpicture}
\end{center}
\end{document}
• Thanks for the answer. This does work when using the article class. But I usually use standalone with the crop option to create the figure. Here, the center environment has no effect, as the picture is cropped at the bounding box. The overlay style also makes it difficult to scale the whole picture to a certain width. The best approach would propably be to define a fixed distance between the axes and the bounding box, but I don't see how... – Janek Apr 1 '14 at 10:10
• Did you take a look at the section I refer to in the pgfplots manual? There are some more settings, including sepecification of a custom shape as bounding box. I don't have time to look into the finer details now, but keeping the axis width & height fixed, with scale only axis and setting a new bounding box a fixed distance (in cm?) relative to the current axis might do the trick. – hugovdberg Apr 1 '14 at 17:37
• Thanks to your suggestions, I was able to produce a solution. Is there any way I can share the reputation except upvoting your answer? – Janek Apr 4 '14 at 13:04

Thanks to @hugovdberg I was able to find a solution. One can use the calc library to define coordinates relative to the plot's axis and then use \path[use as bounding box] (first coordinate) rectangle (second coordinate); to create the bounding box. The following code will produce the desired result. The bounding box is drawn for illustration.

\documentclass[10pt]{beamer}
\usepackage[english]{babel}
\usepackage{pgfplots}
\usetikzlibrary{calc}
\pgfplotsset{compat=1.9}
\begin{document}
\begin{frame}
\begin{tikzpicture}
\begin{axis}[
width=150pt,
height=150pt,
xmin=0,xmax=1,ymin=0,ymax=1,
xlabel=xlabel,
ylabel=ylabel,
scale only axis,
]
(0.1,0.1)
(0.9,0.9)
};
\end{axis}
\coordinate (topleft) at ($(current axis.north west) + (-1.3cm,0.3cm)$);
\coordinate (bottomright) at ($(current axis.south east) + (0.4cm,-1cm)$);
\path[use as bounding box,draw] (topleft) rectangle (bottomright);
\end{tikzpicture}
\end{frame}
\begin{frame}
\begin{tikzpicture}
\begin{axis}[
width=150pt,
height=150pt,
xmin=0,xmax=100,ymin=0,ymax=100,
xlabel=x$_l^a$bel,
ylabel=y$_l^a$bel,
scale only axis,
]
\coordinate (topleft) at ($(current axis.north west) + (-1.3cm,0.3cm)$);
\coordinate (bottomright) at ($(current axis.south east) + (0.4cm,-1cm)$);