将垂直居中的图例放置在组图的右侧

将垂直居中的图例放置在组图的右侧

groupplots我想将图例放置在使用包的右侧pgfplots,并使图例垂直对齐在中心。换句话说,我希望将图例放置在“将图例放在此处”的位置。

问题: 我该怎么做呢?

\documentclass[margin=2mm]{standalone}

\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}

\pgfplotsset{compat=newest}

\begin{document}
\begin{tikzpicture}[shorten >=4pt,shorten <=4pt]
  \begin{groupplot}[
    group style={
      group size=2 by 2,
      x descriptions at=edge bottom,
      y descriptions at=edge left,
    },
    height=3.5cm,width=3.5cm,/tikz/font=\small,
    xlabel=time $t$ / h,
    ylabel=$c$ / mol/L,
    ]
    \nextgroupplot% 1
    \addplot coordinates {(0,1) (1,0)};
    \addplot coordinates {(0,0) (1,1)};
    \nextgroupplot% 2
    \addplot coordinates {(0,1) (1,0)};
    \addplot coordinates {(0,0) (1,1)};
    \nextgroupplot% 3
    \addplot coordinates {(0,1) (1,0)};
    \addplot coordinates {(0,0) (1,1)};
    \nextgroupplot% 4
    \addplot coordinates {(0,1) (1,0)};
    \addplot coordinates {(0,0) (1,1)};
  \end{groupplot}
  \node [right=5mm,anchor=west] at
    ($(group c2r1.south east)!0.5!(group c2r2.north east)$) {Put the legend here};
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案1

要将图例添加到图表右侧的某个位置,需要在环境中添加图例样式,groupplot其中 at=() 是关键。(0,0) 表示角的左下角,而 (1,1) 表示右上角。此类系统axis description cs在第 103 页有记录。

[legend style={at={(1.03,1.4)}, anchor=north west}

在此处输入图片描述

代码:

\documentclass[margin=2mm]{standalone}

\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}

\pgfplotsset{compat=newest}

\begin{document}
\begin{tikzpicture}[shorten >=4pt,shorten <=4pt]
  \begin{groupplot}[
    group style={
      group size=2 by 2,
      x descriptions at=edge bottom,
      y descriptions at=edge left,
    },
    height=3.5cm,width=3.5cm,/tikz/font=\small,
    xlabel=time $t$ / h,
    ylabel=$c$ / mol/L,
    ]
    \nextgroupplot% 1
    \addplot coordinates {(0,1) (1,0)};
    \addplot coordinates {(0,0) (1,1)};
    \nextgroupplot% 2
    \addplot coordinates {(0,1) (1,0)};
    \addplot coordinates {(0,0) (1,1)};
    \nextgroupplot% 3
    \addplot coordinates {(0,1) (1,0)};
    \addplot coordinates {(0,0) (1,1)};
    \nextgroupplot[legend style={at={(1.03,1.4)}, anchor=north west}]% 4
    \addplot coordinates {(0,1) (1,0)};
    \addplot coordinates {(0,0) (1,1)};
\legend{Put the legend here};
  \end{groupplot}
%  \node [right=5mm,anchor=west] at
%    ($(group c2r1.south east)!0.5!(group c2r2.north east)$) {}
\end{tikzpicture}
\end{document}

更新: OP 有一个后续内容,此更新添加了更多信息。这是 的原理groupplot\nextgroupplot[<axis options>]选项是提供给以下轴的选项,直到\nextgroupplotTEX 看到下一个命令。图形以锯齿形顺序排版,这意味着最右边的图形是第 3、第 6 和第 9 个图形。由于 OP 希望图例垂直居中,因此此解决方案使用第 9 个图形作为参考点并指定坐标at=(<x,y>)axis description cs仍然有效。只需注意,对于外部图例x>1y>1

在此处输入图片描述

代码

\documentclass[margin=2mm]{standalone}

\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}

\pgfplotsset{compat=newest}

\begin{document}
\begin{tikzpicture}[shorten >=4pt,shorten <=4pt]
  \begin{groupplot}[
    group style={
      group size=3 by 3,
      x descriptions at=edge bottom,
      y descriptions at=edge left,
    },
    height=3.5cm,width=3.5cm,/tikz/font=\small,
    xlabel=time $t$ / h,
    ylabel=$c$ / mol/L,
    ]
    \nextgroupplot% 1
    \addplot coordinates {(0,1) (1,0)};
    \addplot coordinates {(0,0) (1,1)};
    \nextgroupplot% 2
    \addplot coordinates {(0,1) (1,0)};
    \addplot coordinates {(0,0) (1,1)};
    \nextgroupplot% 3
    \addplot coordinates {(0,1) (1,0)};
    \addplot coordinates {(0,0) (1,1)};
    \nextgroupplot% 4
    \addplot coordinates {(0,1) (1,0)};
    \addplot coordinates {(0,0) (1,1)};
    \nextgroupplot% 5
    \addplot coordinates {(0,1) (1,0)};
    \addplot coordinates {(0,0) (1,1)};
    \nextgroupplot% 6
    \addplot coordinates {(0,1) (1,0)};
    \addplot coordinates {(0,0) (1,1)};
    \nextgroupplot% 7
    \addplot coordinates {(0,1) (1,0)};
    \addplot coordinates {(0,0) (1,1)};
    \nextgroupplot% 8
    \addplot coordinates {(0,1) (1,0)};
    \addplot coordinates {(0,0) (1,1)};
    \nextgroupplot[legend style={at={(1.1,2.2)}, anchor=north west}]% 9   
    \addplot coordinates {(0,1) (1,0)};
    \addplot coordinates {(0,0) (1,1)};
    \legend{Put the legend here};
  \end{groupplot}
%  \node [right=5mm,anchor=west] at
%    ($(group c2r1.south east)!0.5!(group c2r2.north east)$) {}
\end{tikzpicture}
\end{document}

相关内容