2

So I will begin this question by linking to an existing unanswered question on the same matter: Link. I am unsure if I should revive the old question or post a new one, so I just ended up making a new post.

So the problem is that when you use the option \begin{tikzpicture}[trim axis left, trim axis right] along with a reference to a legend through the use of legend to name and \ref{} or \pgfplotslegendfromname{}, the legend position will be messed up.

I have provided an MWE below illustrating the issue.

Has anyone been able to find a good solution to this problem?

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.6}
\usepgfplotslibrary{groupplots}

\begin{document}

\begin{figure} \centering \begin{tikzpicture}[trim axis left, trim axis right]%used for centering only axis and not ylabels etc \begin{groupplot}[group style={group size=1 by 2, vertical sep=2.5cm},small]

\nextgroupplot[title=Plot 1, xlabel=$x$, ylabel=$y_1$,
legend entries={Entry 1,Entry 2,Entry 3}, legend to name=myLegends1, legend columns=3]
\addplot+[domain=0:360] {sin(x)};
\addplot+[domain=0:360] {sin(x)+1};
\addplot+[domain=0:360] {sin(x)+2};

\nextgroupplot[title=Plot 2, xlabel=$x$, ylabel=$y_2$]
\addplot+[domain=0:360] {cos(x)};
\end{groupplot}

\node[anchor=north] at (group c1r1.below south) {\pgfplotslegendfromname{myLegends1}};

\end{tikzpicture}%

\end{figure}

\begin{figure} \centering \begin{tikzpicture}%[trim axis left, trim axis right]%used for centering only axis and not ylabels etc \begin{groupplot}[group style={group size=1 by 2, vertical sep=2.5cm}, small]

\nextgroupplot[title=Plot 1, xlabel=$x$, ylabel=$y_1$,
legend entries={Entry 1,Entry 2,Entry 3}, legend to name=myLegends2, legend columns=3]

\addplot+[domain=0:360] {sin(x)};
\addplot+[domain=0:360] {sin(x)+1};
\addplot+[domain=0:360] {sin(x)+2};

\nextgroupplot[title=Plot 2, xlabel=$x$, ylabel=$y_2$]
\addplot+[domain=0:360] {cos(x)};
\end{groupplot}

\node[anchor=north] at (group c1r1.below south) {\pgfplotslegendfromname{myLegends2}};

\end{tikzpicture}%

\end{figure}

\end{document}

With trim axis (note the legend placement) Without trim axis (here the legend is placed correctly

The first figure shows the plot when the ´trim axis´ is active (here the legend is placed wrong). The second figure shows the plot without the trim axis and here the legend is placed correctly.

HGC
  • 21

1 Answers1

2

It seems that [trim axis] affects all nodes, including the one with the legend in it.

An additional fix was added to center the bounding box by adding additional space to the right side.

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.6}
\usepgfplotslibrary{groupplots}

\begin{document}

\begin{figure} \centering \begin{tikzpicture} \begin{groupplot}[group style={group size=1 by 2, vertical sep=2.5cm},small, trim axis left, trim axis right]

\nextgroupplot[title=Plot 1, xlabel=$x$, ylabel=$y_1$,
legend entries={Entry 1,Entry 2,Entry 3}, legend to name=myLegends1, legend columns=3]
\addplot+[domain=0:360] {sin(x)};
\addplot+[domain=0:360] {sin(x)+1};
\addplot+[domain=0:360] {sin(x)+2};

\nextgroupplot[title=Plot 2, xlabel=$x$, ylabel=$y_2$]
\addplot+[domain=0:360] {cos(x)};
\end{groupplot}

\path (current bounding box.west);% orgin at axis left
\pgfgetlastxy{\xaxis}{\yaxis}%
\path (current bounding box.east) ++(-\xaxis,0);% add left margin to right

\node[anchor=north] (test) at (group c1r1.below south) {\pgfplotslegendfromname{myLegends1}};
\draw[red] (test.south west) rectangle (test.north east);
\draw[red] (current bounding box.south west) rectangle (current bounding box.north east);
\end{tikzpicture}%

\end{figure}

\end{document}


This version uses \pgfextractx instead of \pgfgetlastxy (better , but ugly).

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.6}
\usepgfplotslibrary{groupplots}

\newlength{\xleft} \newlength{\xright}

\begin{document}

\begin{figure} \centering \begin{tikzpicture} \begin{groupplot}[group style={group size=1 by 2, vertical sep=2.5cm},small, trim axis left, trim axis right]

\nextgroupplot[title=Plot 1, xlabel=$x$, ylabel=$y_1$,
legend entries={Entry 1,Entry 2,Entry 3}, legend to name=myLegends1, legend columns=3]
\addplot+[domain=0:360] {sin(x)};
\addplot+[domain=0:360] {sin(x)+1};
\addplot+[domain=0:360] {sin(x)+2};

\nextgroupplot[title=Plot 2, xlabel=$x$, ylabel=$y_2$]
\addplot+[domain=0:360] {cos(x)};
\end{groupplot}

\pgfextractx{\xleft}{\pgfpointdiff{\pgfpointanchor{current bounding box}{west}}
  {\pgfpointanchor{group c1r1}{west}}}% current left margin
\pgfextractx{\xright}{\pgfpointdiff{\pgfpointanchor{group c1r1}{east}}
  {\pgfpointanchor{current bounding box}{east}}}% current right margin
\ifdim \xleft>\xright
  \path (current bounding box.east) ++(\xleft-\xright,0);% add left margin to right
\else
  \path (current bounding box.west) ++(\xleft-\xright,0);% add right margin to left
\fi

\node[anchor=north] (test) at (group c1r1.below south) {\pgfplotslegendfromname{myLegends1}};
\draw[red] (test.south west) rectangle (test.north east);
\draw[red] (current bounding box.south west) rectangle (current bounding box.north east);
\end{tikzpicture}%

\end{figure}

\end{document}

John Kormylo
  • 79,712
  • 3
  • 50
  • 120
  • 1
    Can you explain a little the changes you made ? – projetmbc Aug 13 '20 at 16:24
  • It unfortunately seems like trim axis has no effect when used as done in the example by John Kormylo. The purpose of using trim axis, for me at least, is to make it possible to center just the axis instead of the entire picture (i.e., the axis with labels, ticks, etc.). If you add a caption to the figure environment, it will be easy to see that see that John's solution does not center the picture correctly (i.e., that it is equivalent to the case where no trim axis is performed at all). – HGC Aug 14 '20 at 06:36
  • The fix was to move the trim axis parameters to the groupplot, not the entire tikzpicture. The \draw was a debug tool. Originally it showed that the node anchors were in the right place, only the text was offset. – John Kormylo Aug 14 '20 at 14:30
  • Moving the trim axis parameters to the tikzpicture has no effect on the bounding box either. – John Kormylo Aug 14 '20 at 15:37