在 pgfplots 中移动 y 轴标签和图例的位置

在 pgfplots 中移动 y 轴标签和图例的位置

我想将其中一个 y 标签移动到右侧的第二组图,并将图例移动到图的中心,如下所示:

在此处输入图片描述

以下是代码:

\documentclass[11pt]{book}
\usepackage[%
    ,top=3cm
    ,bottom=3cm
    ,left=3.2cm
    ,right=3.2cm
    ,headsep=10pt
    ,a4paper
    ]{geometry}
\usepackage{lipsum}
\usepackage{pgfplots,pgfplotstable, booktabs}
\usetikzlibrary{pgfplots.groupplots, matrix}
\usepgfplotslibrary{groupplots}
\pgfplotsset{%
    compat=1.12,
    minor grid style={dashed,red},
    major grid style={dotted,green!50!black},
    select coords between index/.style 2 args={
    x filter/.code={
        \ifnum\coordindex<#1\def\pgfmathresult{}\fi
        \ifnum\coordindex>#2\def\pgfmathresult{}\fi
    }
}}
\usepackage{subfig}
\captionsetup[subfigure]{labelfont={it,bf},textfont={it,bf},labelformat=parens,labelsep=space}
\usepackage{siunitx}
\usepackage{listings}
\usepackage{filecontents}

\begin{filecontents*}{array.csv}
A   B   C   D
1   -10 -15 -20
2   -20 -20 -25
3   -30 -25 -30
4   -40 -30 -35
5   -50 -35 -40
6   -60 -40 -45
7   -70 -45 -50
8   -80 -50 -55
9   -90 -55 -60
10  -100    -60 -65
11  -110    -65 -70
12  -120    -70 -75
13  -130    -75 -80
14  -140    -80 -85
15  -150    -85 -90
16  -160    -90 -95
17  -170    -95 -100
18  -180    -100    -105
19  -190    -105    -110
20  -200    -110    -115
21  -210    -115    -120
22  -220    -120    -125
23  -230    -125    -130
24  -240    -130    -135
25  -250    -135    -140
26  -260    -140    -145
27  -270    -145    -150
28  -280    -150    -155
29  -290    -155    -160
30  -300    -160    -165
\end{filecontents*}

\begin{document}

\begin{figure}
\centering
\begin{tikzpicture}
\setcaptionsubtype
\begin{groupplot}[%
            ,group style={%
                ,group name=my plots
                ,group size=2 by 2
                ,vertical sep=2cm,
                ,horizontal sep = 2cm,
                ,ylabels at=edge left
            }
            ,width=7cm
            ,height=6cm
            ,try min ticks=5
            ,xlabel={\bfseries{\emph{Time in Secs}}}
            ,grid=both
            ,every major grid/.style={gray, opacity=0.5}
            ]
\nextgroupplot%
\addplot[select coords between index={0}{9},mark=*,red,mark options={scale=.65}]table[x index=0,y index=1,col sep=space] {array.csv};\label{plots:InstA}
\addplot[select coords between index={0}{9},mark=*,blue,mark options={scale=.65}]table[x index=0,y index=2,col sep=space] {array.csv};\label{plots:InstB}
\addplot[select coords between index={0}{9},cyan]table[x index=0,y index=3,col sep=space] {array.csv};\label{plots:InstC}

\coordinate (top) at (rel axis cs:0,1);

\nextgroupplot%
\addplot[select coords between index={10}{19},mark=*,red,mark options={scale=.65}]table[x index=0,y index=1,col sep=space] {array.csv};
\addplot[select coords between index={10}{19},mark=*,blue,mark options={scale=.65}]table[x index=0,y index=2,col sep=space] {array.csv};
\addplot[select coords between index={10}{19},cyan]table[x index=0,y index=3,col sep=space] {array.csv};

\coordinate (top2) at (rel axis cs:0,1);

\nextgroupplot%
\addplot[select coords between index={20}{24},mark=*,red,mark options={scale=.65}]table[x index=0,y index=1,col sep=space] {array.csv};
\addplot[select coords between index={20}{24},mark=*,blue,mark options={scale=.65}]table[x index=0,y index=2,col sep=space] {array.csv};
\addplot[select coords between index={20}{24},cyan]table[x index=0,y index=3,col sep=space] {array.csv};

\coordinate (bot) at (rel axis cs:1,0);

\nextgroupplot%
\addplot[select coords between index={25}{29},mark=*,red,mark options={scale=.65}]table[x index=0,y index=1,col sep=space] {array.csv};
\addplot[select coords between index={25}{29},mark=*,blue,mark options={scale=.65}]table[x index=0,y index=2,col sep=space] {array.csv};
\addplot[select coords between index={25}{29},cyan]table[x index=0,y index=3,col sep=space] {array.csv};

\coordinate (bot2) at (rel axis cs:2,0);

\end{groupplot}

\path (top-|current bounding box.west)--
          node[anchor=south,rotate=90] {\large{\bf{Testing of Parameters}}}
          (bot-|current bounding box.west);

\path (top2-|current bounding box.west)--
          node[anchor=south,rotate=90] {\large{\bf{Testing of Parameters}}}
          (bot2-|current bounding box.west);
% legend
\path (top|-current bounding box.south)--
      coordinate(legendpos)
      (bot|-current bounding box.south);
\matrix[
    matrix of nodes,
    anchor=south,
    draw,
    inner sep=0.2em,
    draw
  ]at([yshift=-3ex]legendpos)
  {
    \ref{plots:InstA}& \footnotesize{InstA}&[5pt]
    \ref{plots:InstB}& \footnotesize{InstB}&[5pt]
    \ref{plots:InstC}& \footnotesize{InstC}&[5pt]\\};

\node[text width=.5\linewidth,align=center,anchor=south] at (my plots c1r1.north) {\caption[]{Test 1\label{subplot:one}}};
\node[text width=.5\linewidth,align=center,anchor=south] at (my plots c2r1.north) {\caption[]{Test 2\label{subplot:two}}};
\node[text width=.5\linewidth,align=center,anchor=south] at (my plots c1r2.north) {\caption[]{Test 3\label{subplot:three}}};
\node[text width=.5\linewidth,align=center,anchor=south] at (my plots c2r2.north) {\caption[]{Test 4\label{subplot:four}}};
\end{tikzpicture}
\caption[]{Comparison of different tests.}\label{abserror}
\end{figure}
\end{document}

答案1

您已将组图命名为,group name=my plots因此您可以使用类似坐标,my plots c1r1.north east而不必定义新坐标。这样,定位标签和图例就更容易了。

\path [nodes={anchor=south,rotate=90,font=\large\bfseries,midway}]
  (my plots c1r1.outer north west)--(my plots c1r2.outer south west)
    node {Testing of Parameters 1}
  (my plots c2r1.outer north west)--(my plots c2r2.outer south west)
    node {Testing of Parameters 2}
;
% legend
\path (my plots c1r1.west|-current bounding box.south)--
      coordinate(legendpos)
      (my plots c2r2.east|-current bounding box.south);
\matrix[
    matrix of nodes,
    anchor=south,
    draw,
    inner sep=0.2em,
    draw
  ]at([yshift=-4ex]legendpos)
  {
    \ref{plots:InstA}& \footnotesize{InstA}&[5pt]
    \ref{plots:InstB}& \footnotesize{InstB}&[5pt]
    \ref{plots:InstC}& \footnotesize{InstC}&[5pt]\\};

在此处输入图片描述

代码:

\begin{filecontents*}{array.csv}
A   B   C   D
1   -10 -15 -20
2   -20 -20 -25
3   -30 -25 -30
4   -40 -30 -35
5   -50 -35 -40
6   -60 -40 -45
7   -70 -45 -50
8   -80 -50 -55
9   -90 -55 -60
10  -100    -60 -65
11  -110    -65 -70
12  -120    -70 -75
13  -130    -75 -80
14  -140    -80 -85
15  -150    -85 -90
16  -160    -90 -95
17  -170    -95 -100
18  -180    -100    -105
19  -190    -105    -110
20  -200    -110    -115
21  -210    -115    -120
22  -220    -120    -125
23  -230    -125    -130
24  -240    -130    -135
25  -250    -135    -140
26  -260    -140    -145
27  -270    -145    -150
28  -280    -150    -155
29  -290    -155    -160
30  -300    -160    -165
\end{filecontents*}
\documentclass[11pt]{book}
\usepackage[%
    ,top=3cm
    ,bottom=3cm
    ,left=3.2cm
    ,right=3.2cm
    ,headsep=10pt
    ,a4paper
    ]{geometry}
\usepackage{lipsum}
\usepackage{pgfplots,pgfplotstable, booktabs}
\usetikzlibrary{pgfplots.groupplots, matrix}
\usepgfplotslibrary{groupplots}
\pgfplotsset{%
    compat=1.12,
    minor grid style={dashed,red},
    major grid style={dotted,green!50!black},
    select coords between index/.style 2 args={
    x filter/.code={
        \ifnum\coordindex<#1\def\pgfmathresult{}\fi
        \ifnum\coordindex>#2\def\pgfmathresult{}\fi
    }
}}
\usepackage{subfig}
\captionsetup[subfigure]{labelfont={it,bf},textfont={it,bf},labelformat=parens,labelsep=space}

\begin{document}

\begin{figure}
\centering
\begin{tikzpicture}
\setcaptionsubtype
\begin{groupplot}[%
            ,group style={%
                ,group name=my plots
                ,group size=2 by 2
                ,vertical sep=2cm,
                ,horizontal sep = 2cm,
                ,ylabels at=edge left
            }
            ,width=7cm
            ,height=6cm
            ,try min ticks=5
            ,xlabel={\bfseries{\emph{Time in Secs}}}
            ,grid=both
            ,every major grid/.style={gray, opacity=0.5}
            ]
\nextgroupplot%
\addplot[select coords between index={0}{9},mark=*,red,mark options={scale=.65}]table[x index=0,y index=1,col sep=space] {array.csv};\label{plots:InstA}
\addplot[select coords between index={0}{9},mark=*,blue,mark options={scale=.65}]table[x index=0,y index=2,col sep=space] {array.csv};\label{plots:InstB}
\addplot[select coords between index={0}{9},cyan]table[x index=0,y index=3,col sep=space] {array.csv};\label{plots:InstC}

\nextgroupplot%
\addplot[select coords between index={10}{19},mark=*,red,mark options={scale=.65}]table[x index=0,y index=1,col sep=space] {array.csv};
\addplot[select coords between index={10}{19},mark=*,blue,mark options={scale=.65}]table[x index=0,y index=2,col sep=space] {array.csv};
\addplot[select coords between index={10}{19},cyan]table[x index=0,y index=3,col sep=space] {array.csv};

\nextgroupplot%
\addplot[select coords between index={20}{24},mark=*,red,mark options={scale=.65}]table[x index=0,y index=1,col sep=space] {array.csv};
\addplot[select coords between index={20}{24},mark=*,blue,mark options={scale=.65}]table[x index=0,y index=2,col sep=space] {array.csv};
\addplot[select coords between index={20}{24},cyan]table[x index=0,y index=3,col sep=space] {array.csv};

\nextgroupplot%
\addplot[select coords between index={25}{29},mark=*,red,mark options={scale=.65}]table[x index=0,y index=1,col sep=space] {array.csv};
\addplot[select coords between index={25}{29},mark=*,blue,mark options={scale=.65}]table[x index=0,y index=2,col sep=space] {array.csv};
\addplot[select coords between index={25}{29},cyan]table[x index=0,y index=3,col sep=space] {array.csv};

\end{groupplot}

\path [nodes={anchor=south,rotate=90,font=\large\bfseries,midway}]
  (my plots c1r1.outer north west)--(my plots c1r2.outer south west)
    node {Testing of Parameters 1}
  (my plots c2r1.outer north west)--(my plots c2r2.outer south west)
    node {Testing of Parameters 2}
;
% legend
\path (my plots c1r1.west|-current bounding box.south)--
      coordinate(legendpos)
      (my plots c2r2.east|-current bounding box.south);
\matrix[
    matrix of nodes,
    anchor=south,
    draw,
    inner sep=0.2em,
    draw
  ]at([yshift=-4ex]legendpos)
  {
    \ref{plots:InstA}& \footnotesize{InstA}&[5pt]
    \ref{plots:InstB}& \footnotesize{InstB}&[5pt]
    \ref{plots:InstC}& \footnotesize{InstC}&[5pt]\\};


\node[text width=.5\linewidth,align=center,anchor=south] at (my plots c1r1.north) {\caption[]{Test 1\label{subplot:one}}};
\node[text width=.5\linewidth,align=center,anchor=south] at (my plots c2r1.north) {\caption[]{Test 2\label{subplot:two}}};
\node[text width=.5\linewidth,align=center,anchor=south] at (my plots c1r2.north) {\caption[]{Test 3\label{subplot:three}}};
\node[text width=.5\linewidth,align=center,anchor=south] at (my plots c2r2.north) {\caption[]{Test 4\label{subplot:four}}};
\end{tikzpicture}
\caption[]{Comparison of different tests.}\label{abserror}
\end{figure}
\end{document}

相关内容