# Problem aligning multiple pgfplots due to different axis label sizes

I have two plots that comes back to back in my paper. One of these plots is generated from 1000 data samples while the other one from 320. The problem that I'm, facing right now, is that plots are aligned by right-most axis labels (1,000 and 320). This result in a situation where actual right side border of the plots are not aligned. To make it clear, please consider the following MWE:

\documentclass[11pt,twocolumn]{article}
\usepackage{lipsum}
\usepackage{pgfplots}
\usepackage{tikz}

\begin{document}
\lipsum[1-3]
\begin{figure}[t]
\centering
\resizebox{\linewidth}{!}{
\begin{tikzpicture}
\def\UpperBound{2.112528}
\def\LowerBound{-0.092346}
\begin{axis}[
scale only axis,
xmin             = 0,
xmax             = 1000,
ymin             = -0.292346,
ymax             = 2.312528,
y                = 30,
width            = 0.5\textwidth,
height           = 0.25\textwidth,
grid style       = {dashed, ultra thin},
grid             = major,
xtick            = {0,250,...,1000},
ytick            = \empty,
tick label style = {font=\small}
]
\addplot[thick, blue] table [x expr=\coordindex, y index=0]{values.csv};

\draw[thin, green, dashed]  (axis cs:\pgfkeysvalueof{/pgfplots/xmin},\UpperBound) --
(axis cs:\pgfkeysvalueof{/pgfplots/xmax},\UpperBound);
\draw[thin, red, dashed]    (axis cs:\pgfkeysvalueof{/pgfplots/xmin},\LowerBound) --
(axis cs:\pgfkeysvalueof{/pgfplots/xmax},\LowerBound);
\end{axis}
\end{tikzpicture}
}
\caption{A caption.}
\end{figure}

\begin{figure}
\centering
\resizebox{\linewidth}{!}{
\begin{tikzpicture}
\begin{axis}[
scale only axis,
grid             = major,
xmin             = 0,
xmax             = 320,
width            = 0.5\textwidth,
height           = 0.15\textwidth,
grid style       = {dashed, ultra thin},
xtick            = {0,80,...,320},
ytick            = \empty,
tick label style = {font=\small}
]
\addplot[thick, blue] table [x expr=\coordindex, y index=0]{error_file.csv};
\end{axis}
\end{tikzpicture}
}
\caption{Another caption.}
\end{figure}
\end{document}


resulting in:

Is there any option to bring the right-most value inside the canvas of the plot such that outer most of the plot becomes the right border? (and other labels as well) Or perhaps rotate the labels like gnuplots (although I prefer not to resort to the latter).

Data for MWE File1, File2

• Would it be an option to let the labels protrude over the right edge of the plot area, making the right edge of the plot area flush with the text area?
– Jake
Jun 3, 2014 at 11:48
• @Jake, not sure if I got what you mean but if it aligns my plots it would be fine! I actually don't like any of my own ideas, so please don't hesitate to come up with your solution to the problem. Jun 3, 2014 at 12:11

By adding trim axis right to the tikzpicture options, everything that protrudes past the right edge of the plot area will be ignored for calculating the size of the plot and for positioning the plot on the page. That way, you allow the rightmost labels to protrude into the margin of your document, which may or may not be acceptable for you:

\documentclass[11pt,twocolumn]{article}
\usepackage{lipsum}
\usepackage{pgfplots}
\usepackage{tikz}

\begin{document}
\lipsum[1-3]
\begin{figure}[t]
\centering
\resizebox{\linewidth}{!}{
\begin{tikzpicture}[trim axis right]
\def\UpperBound{2.112528}
\def\LowerBound{-0.092346}
\begin{axis}[
scale only axis,
xmin             = 0,
xmax             = 1000,
ymin             = -0.292346,
ymax             = 2.312528,
y                = 30,
width            = 0.5\textwidth,
height           = 0.25\textwidth,
grid style       = {dashed, ultra thin},
grid             = major,
xtick            = {0,250,...,1000},
ytick            = \empty,
tick label style = {font=\small}
]

\draw[thin, green, dashed]  (axis cs:\pgfkeysvalueof{/pgfplots/xmin},\UpperBound) --
(axis cs:\pgfkeysvalueof{/pgfplots/xmax},\UpperBound);
\draw[thin, red, dashed]    (axis cs:\pgfkeysvalueof{/pgfplots/xmin},\LowerBound) --
(axis cs:\pgfkeysvalueof{/pgfplots/xmax},\LowerBound);
\end{axis}
\end{tikzpicture}
}
\caption{A caption.}
\end{figure}

\begin{figure}
\centering
\resizebox{\linewidth}{!}{
\begin{tikzpicture}[trim axis right]
\begin{axis}[
scale only axis,
grid             = major,
xmin             = 0,
xmax             = 320,
width            = 0.5\textwidth,
height           = 0.15\textwidth,
grid style       = {dashed, ultra thin},
xtick            = {0,80,...,320},
ytick            = \empty,
tick label style = {font=\small}
]
\end{axis}
\end{tikzpicture}
}
\caption{Another caption.}
\end{figure}
\end{document}


You can set the xticklabels explicitly using \llap for the last xticklabel

xtick            = {0,250,...,1000},
xticklabels      ={0,250,...,750,\llap{1000}},


and

xtick            = {0,80,...,320},
xticklabels      = {0,80,...,240,\llap{320}},


Code

\documentclass[11pt,twocolumn]{article}
\usepackage{lipsum}
\usepackage{pgfplots}

\pgfplotsset{
last xtick/.style={
extra x ticks      = #1,
extra x tick style = {tick label style={anchor=north east,inner xsep=0pt}}
}
}

\begin{document}
\lipsum[1-3]
\begin{figure}[t]
\centering
\resizebox{\linewidth}{!}{
\begin{tikzpicture}
\def\UpperBound{2.112528}
\def\LowerBound{-0.092346}
\begin{axis}[
scale only axis,
xmin             = 0,
xmax             = 1000,
ymin             = -0.292346,
ymax             = 2.312528,
y                = 30,
width            = 0.5\textwidth,
height           = 0.25\textwidth,
grid style       = {dashed, ultra thin},
grid             = major,
xtick            = {0,250,...,1000},
xticklabels      ={0,250,...,750,\llap{1000}},
ytick            = \empty,
tick label style = {font=\small},
]
\addplot[thick, blue] table [x expr=\coordindex, y index=0]{values.csv};

\draw[thin, green, dashed]  (axis cs:\pgfkeysvalueof{/pgfplots/xmin},\UpperBound) --
(axis cs:\pgfkeysvalueof{/pgfplots/xmax},\UpperBound);
\draw[thin, red, dashed]    (axis cs:\pgfkeysvalueof{/pgfplots/xmin},\LowerBound) --
(axis cs:\pgfkeysvalueof{/pgfplots/xmax},\LowerBound);
\end{axis}
\end{tikzpicture}
}
\caption{A caption.}
\end{figure}

\begin{figure}
\centering
\resizebox{\linewidth}{!}{
\begin{tikzpicture}
\begin{axis}[
scale only axis,
grid             = major,
xmin             = 0,
xmax             = 320,
width            = 0.5\textwidth,
height           = 0.15\textwidth,
grid style       = {dashed, ultra thin},
xtick            = {0,80,...,320},
xticklabels      = {0,80,...,240,\llap{320}},
last xtick       = 320,
ytick            = \empty,
tick label style = {font=\small}
]
\addplot[thick, blue] table [x expr=\coordindex, y index=0]{error_file.csv};
\end{axis}
\end{tikzpicture}
}
\caption{Another caption.}
\end{figure}
\end{document}


Or if there are no other extra x ticks you can use this option to define a different style for the last xtick:

\pgfplotsset{
last xtick/.style={
extra x ticks = #1,
extra x tick style = {tick label style={anchor=north east,inner xsep=0pt}}
}
}


and then for the first plot

xtick            = {0,250,...,750},
last xtick       = 1000,


and for the second plot

xtick            = {0,80,...,300},
last xtick       = 320,


An alternative is use of scaled x ticks=base 10:-4 and 10:-2, respectively in the axis options.

Code

\documentclass[11pt,twocolumn]{article}
\usepackage{lipsum}
\usepackage{pgfplots}
\usepackage{tikz}

\begin{document}
\lipsum[1-3]
\begin{figure}[t]
\centering
\resizebox{\linewidth}{!}{
\begin{tikzpicture}
\def\UpperBound{2.112528}
\def\LowerBound{-0.092346}
\begin{axis}[
scale only axis,
xmin             = 0,
xmax             = 1000,
ymin             = -0.292346,
ymax             = 2.312528,
y                = 30,
width            = 0.5\textwidth,
height           = 0.25\textwidth,
grid style       = {dashed, ultra thin},
grid             = major,
xtick            = {0,250,...,1000},
ytick            = \empty,
tick label style = {font=\small},
scaled x ticks=base 10:-4,
]
\addplot[thick, blue] table [x expr=\coordindex, y index=0]{values.csv};
\draw[thin, green, dashed]  (axis cs:\pgfkeysvalueof{/pgfplots/xmin},\UpperBound) -- (axis cs:\pgfkeysvalueof{/pgfplots/xmax},\UpperBound);
\draw[thin, red, dashed]    (axis cs:\pgfkeysvalueof{/pgfplots/xmin},\LowerBound) -- (axis cs:\pgfkeysvalueof{/pgfplots/xmax},\LowerBound);
\end{axis}
\end{tikzpicture}
}
\caption{A caption.}
\end{figure}

\begin{figure}
\centering
\resizebox{\linewidth}{!}{
\begin{tikzpicture}
\begin{axis}[
scale only axis,
grid             = major,
xmin             = 0,
xmax             = 320,
width            = 0.5\textwidth,
height           = 0.15\textwidth,
grid style       = {dashed, ultra thin},
xtick            = {0,80,...,320},
ytick            = \empty,
tick label style = {font=\small},
scaled x ticks=base 10:-2,
]
\addplot[thick, blue] table [x expr=\coordindex, y index=0]{error_file.csv};
\end{axis}
\end{tikzpicture}
}
\caption{Another caption.}
\end{figure}
\end{document}

• But the alignment would still be slightly off in this case (3.2 protrudes further than 10)
– Jake
Jun 3, 2014 at 12:42
• Indeed, Jake, thanks. In such case, I would suggest 10:-4 for the first plot then two plots will have the same formats. Jun 3, 2014 at 12:50