# subfloat and pgfplot with trim

I am using subfloat and pgfplot to place two plots side by side. I want the subfloat caption to centre aligned with the axis so I use trim axis. But then my plots label goes out of the margin.

\begin{figure}
\centering
\subfloat[Fig 1]{
\begin{tikzpicture}[trim axis left,trim axis right]
\begin{axis}[
grid=major,
scaled ticks=false,
x post scale={0.8},
y post scale={1.25},
xmin=1,xmax=5,
ymin=0,ymax=0.07,
tick label style={/pgf/number format/fixed},
ylabel style={rotate=-90},
xlabel={r},
ylabel={$\displaystyle \frac{V_{test}}{V_{x}}$},
legend cell align=left,
legend entries={,$\Psi=0$,,$\Psi=2\pi$,,$\Psi=4\pi$}
]

\addplot [thick, blue] table [col sep=comma] {test.csv};
\end{axis}
\end{tikzpicture}
}
\hfill
\subfloat[Fig 2]{
\begin{tikzpicture}[trim axis left,trim axis right]
\begin{axis}[
grid=major,
scaled ticks=false,
x post scale={0.8},
y post scale={1.25},,
xmin=1,xmax=5,
ymin=0,ymax=0.07,
tick label style={/pgf/number format/fixed},
ylabel style={rotate=-90},
xlabel={r},
ylabel={$\displaystyle \frac{V_{test}}{V_{x}}$},
legend cell align=left,
legend entries={,$\Psi=0$,,$\Psi=2\pi$,,$\Psi=4\pi$}
]
\addplot [thick, olive] table [col sep=comma] {test.csv};
\end{axis}
\end{tikzpicture}
}
\caption{Sample image}
\end{figure}


Is it possible to fix it without specifying horizontal alignment in dimensions? Because it will be tedious as I have many plots.

Replace \hfill width \hfil:

with this the bounding box are centered on page width, however the y labels aren't considered in centering (since with trim axis left are excluded).

Test code for above image is (your code snippet is unfortunately useless):

\documentclass{article}
\usepackage{subfig}
\usepackage{pgfplots}
\pgfplotsset{compat=1.13, width=77mm}

\usepackage[showframe]{geometry}% to show page layout

\begin{document}
\begin{figure}[ht]
\pgfplotsset{
grid=major,
scaled ticks=false,
x post scale={0.8},
y post scale={1.25},
legend cell align=left,
legend pos=south east
}
\centering
\subfloat[Fig. 1]{
\begin{tikzpicture}[baseline,trim axis left]
\begin{axis}[
ylabel={$f(x)=x^2$}
]
\legend{$x$},
\end{axis}
\end{tikzpicture}
}
\hfil
\subfloat[Fig. 2]{
\begin{tikzpicture}[baseline,trim axis left]
\begin{axis}[
ylabel={$f(x)=x^2$}
]
\legend{$x^2$},
\end{axis}
\end{tikzpicture}
}
\caption{Sample image}
\end{figure}
\end{document}


This solutions adds extra space to the right of the tikzpicture to center the axis box rather than the bounding box. It uses the name= option of axis to locate the edges.

The main difference between this and using trim axis left is when the plot gets too wide. As you can see, it already forces the second subfloat to a new line. I put frames around the subfloats to show their size.

Note: this is based on Zarlo's MWE. I tried to use yours, but it really was useless.

\documentclass{article}
\usepackage{subfig}
\usepackage{pgfplots}
\pgfplotsset{compat=1.13, width=77mm}

\newlength{\offset}
\newlength{\temp}

\usepackage[showframe]{geometry}% to show page layout
\begin{document}
\begin{figure}[ht]
\pgfplotsset{
grid=major,
scaled ticks=false,
x post scale={0.8},
y post scale={1.25},
legend cell align=left,
legend pos=south east
}
\centering\fbox{%
\subfloat[Fig. 1]{
\begin{tikzpicture}[baseline]
\begin{axis}[name=border,
ylabel={$f(x)=x^2$}
]
\legend{$x$}
\end{axis}
\pgfextractx{\offset}{\pgfpointdiff{\pgfpointanchor{current bounding box}{west}}%
{\pgfpointanchor{border}{west}}}% \pgfpointorigin will do
\pgfextractx{\temp}{\pgfpointdiff{\pgfpointanchor{current bounding box}{east}}%
\end{tikzpicture}\hspace{\offset}% add excess left margin to right
}}%
\hfill\fbox{%
\subfloat[Fig. 2]{
\begin{tikzpicture}[baseline]
\begin{axis}[name=border,
ylabel={$f(x)=x^2$}
]
\legend{$x^2$}