pgfplots:tikzpicture 中的修剪轴弄乱了图例位置

pgfplots:tikzpicture 中的修剪轴弄乱了图例位置

因此,我将通过链接到有关同一问题的现有未解答问题来开始这个问题:关联。我不确定是否应该重新提出旧问题或发布新问题,所以我最终只好发布一个新帖子。

因此问题是,当您通过使用and或使用该选项\begin{tikzpicture}[trim axis left, trim axis right]以及对图例的引用时,图例的位置就会变得混乱。legend to name\ref{}\pgfplotslegendfromname{}

我在下面提供了一个 MWE 来说明该问题。

有谁能找到解决这个问题的好方法吗?

\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}

带修剪轴(注意图例位置) 没有修剪轴(此处图例放置正确

第一个图显示了“修剪轴”处于活动状态时的绘图(此处图例放置错误)。第二个图显示了没有修剪轴的绘图,此处图例放置正确。

答案1

这似乎[trim axis]影响所有节点,包括其中包含图例的节点。

通过在右侧添加额外空间,添加了额外的修复以使边界框居中。

\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}

此版本使用\pgfextractx而不是\pgfgetlastxy(更好,但更丑)。

\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}

相关内容