0

How can I align the y axes of subsequent subfigures horizontally? Like in this test case:

\documentclass{article}

\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{subcaption}

\begin{document}

\begin{figure}[t]
    \pgfplotsset{
        my plot/.style = {
            width = \linewidth,
            height = 4cm,
            axis lines = left
        }
    }
    \centering
    \begin{subfigure}[t]{\linewidth}
        \centering
        \begin{tikzpicture}
            \begin{axis}[my plot]
                \addplot {rand};
            \end{axis}
        \end{tikzpicture}
        \caption{first}
    \end{subfigure}
    \\
    \begin{subfigure}[t]{\linewidth}
        \centering
        \begin{tikzpicture}
            \begin{axis}[my plot]
                \addplot {rand*100};
            \end{axis}
        \end{tikzpicture}
        \caption{second}
    \end{subfigure}
    \\
    \begin{subfigure}[t]{\linewidth}
        \centering
        \begin{tikzpicture}
            \begin{axis}[my plot]
                \addplot {rand*5000};
            \end{axis}
        \end{tikzpicture}
        \caption{third}
    \end{subfigure}
    \caption{test figure}
\end{figure}

\end{document}

Axes should be on top of each other, on the red line, but they are shifted horizontally because of different y tick label widths.

enter image description here

I have a feeling that a combination of extra y ticks and hphantom to accomodate the widest tick label could work, but I haven't been able to figure anything out.

Another possibility I haven't been able to make work is to wrap each label in rlap to collapse their width and right-align them.

The ymin and ymax are known for each of the subfigures, so I can potentially hardcode them if that turns out to be a solution.

  • what looks to be the difference here is the width of the labels on the y axis. if you could force those all to the same width, the axes should align. – barbara beeton May 21 '15 at 12:47
  • Do you have a suggestion for how I could do that? – soapygopher May 21 '15 at 13:45
  • unfortunately, i'm barely a novice tikz user, so i'd have to dig through the manual to figure it out. (i was hoping the suggestion might spur some more experienced user to make an effort.) – barbara beeton May 21 '15 at 13:55
4

I found a solution. Adding the option trim axis left to the tikzpicture chops off everything left of the axis. Combined with \centering before the subfigure this aligns the plots nicely.

\begin{subfigure}[t]{\linewidth}
    \centering
    \begin{tikzpicture}[trim axis left]
        \begin{axis}[my plot]
            ...

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.