3

Using the groupplots library of pgfplots, it is fairly simple to create aligned plots. Now, I want to move the left axes into the margin of my document. Typically, that is, if I had a single plot without groupplots, this could be done with a combination of trim axis left and scale only axis.

Consider the following example. I want to create four plots, aligned in a 2x2 manner.

\documentclass{article}

\usepackage{pgfplots}
\usepackage{lipsum}

\usepgfplotslibrary{groupplots}

\begin{document}
\lipsum[2]

\begin{tikzpicture}[trim axis left]
    \begin{groupplot}[
        group style={group size=2 by 2},
        width=0.499\textwidth,
        scale only axis,
        ]

        \nextgroupplot
        \addplot {x};

        \nextgroupplot
        \addplot {x*x};

        \nextgroupplot
        \addplot {1};

        \nextgroupplot
        \addplot {-x};

    \end{groupplot}
\end{tikzpicture}

\lipsum[2]
\end{document}

Applying trim axis left and scale only axis, however, now trims not only the left axis of the left plots, but all the left plots. Probably this is because every subplot trims everything left of it, which includes the left plots in case of the right ones.

I tried to apply these options locally via the optional argument of \nextgroupplot, but it did not work. There is a similar question about aligning, but I would prefer a solution that includes groupplots. Is there a way to do it?

  • Maybe a dumb question, but is there a specific reason for your figures to be in the margins ? – BambOo Jul 13 '18 at 10:35
  • There is a special version of trim left Mads for groupplots I think, look in the manual. – Torbjørn T. Jul 13 '18 at 10:57
  • @BambOo, there are no special reasons for it, I just happen to like. Personal preferences, you could phrase it. – segenen Jul 13 '18 at 11:19
  • @segenen, OK, I have personal preferences too :). It's just that people sometimes ask questions to solve a problem that actually is the consequence of an underlying problem. – BambOo Jul 13 '18 at 11:41
1

Use trim axis group left, which is tailored for groupplots. In your example, you might also need \noindent, but whether that is needed in your actual document depends, for example, whether the tikzpicture is in a figure environment or not.

If you want the right axis border aligned with the right side of the text as well, you could set the horizontal sep, and then set width to 0.5\textwidth minus half that horizontal sep.

enter image description here

\documentclass{article}

\usepackage{pgfplots}
\usepackage{lipsum}

\usepgfplotslibrary{groupplots}

\begin{document}
\lipsum[2]

\noindent\begin{tikzpicture}[trim axis group left]
    \begin{groupplot}[
        group style={
          group size=2 by 2,
          horizontal sep=30pt
         },
        width=0.5\textwidth-15pt, % subtract half the horizontal sep
        scale only axis,
        ]

        \nextgroupplot
        \addplot {x};

        \nextgroupplot
        \addplot {x*x};

        \nextgroupplot
        \addplot {1};

        \nextgroupplot
        \addplot {-x};

    \end{groupplot}
\end{tikzpicture}

\lipsum[2]
\end{document}
  • Works perfectly, thank you. Is there an elegant way to align the left border with the text, too? – segenen Jul 13 '18 at 11:16
  • @segenen See updated answer for a quick hack. – Torbjørn T. Jul 13 '18 at 11:37

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.