pgfplot 中的常见图例-编译时出现问题

pgfplot 中的常见图例-编译时出现问题

我正在尝试为我拥有的六个图块获取一个共同的图例。我尝试实现找到的代码这里,但无法编译我的程序。我试图将图例放在图的底部,就在标题之前……但我失败了。这是我的代码:

\documentclass[11pt]{book}
\usepackage[top=3cm,bottom=3cm,left=3.2cm,right=3.2cm,headsep=10pt,a4paper]{geometry}
\usepackage{graphicx}
\usepackage{tikz}
\usepackage{float}
\usepackage{pgfplots,pgfplotstable, booktabs}
\usetikzlibrary{pgfplots.groupplots, matrix}
\pgfplotsset{compat=1.8}

\begin{filecontents*}{data.csv}
  Iter   x1   x2      x3      x4      x5      x6
     1, 0.9317521, 1.1849326, 1.6130556, 0.8022207, 0.9317521, 1.1849326
     2, 1.8946202, 1.1228282, 1.8964566, -0.5353802, 1.8946202, 1.1228282
     3, 1.5243302, 1.0372991, 1.4375012, 0.9719003, 1.5243302, 1.0372991
     4, 1.5797030, 1.1346832, 1.8717142, 0.3138737, 1.5797030, 1.1346832
     5, 1.8814457, 1.0529187, 2.0568468, -0.5509391, 1.8814457, 1.0529187
     6, 2.0435003, 1.0470546, 2.0621956, -0.3565483, 2.0435003, 1.0470546
     7, 2.0373926, 1.1215579, 2.1836100, 0.3360301, 2.0373926, 1.1215579
     8, 1.9797077, 1.1632352, 1.8299063, 0.3871091, 1.9797077, 1.1632352
     9, 1.9972528, 1.1952478, 1.8133509, -0.0867033, 1.9972528, 1.1952478
    10, 1.8320176, 1.0625633, 1.0727495, 1.7256738, 1.8320176, 1.0625633 
\end{filecontents*}

\pgfplotsset{minor grid style={dashed,red}}
\pgfplotsset{major grid style={dotted,green!50!black}}

\begin{document}
    \begin{figure}[H]
        \centering
        \begin{tikzpicture}
            \begin{groupplot}[group style={
                              group size=2 by 3,
                              vertical sep=2cm,
                              horizontal sep = 2cm,
                              ylabels at=edge left},
                              width=8cm,
                              height=7cm]
\nextgroupplot[title = {\emph{(b) Plot 1}}, xlabel={$Number~of~Recursions$}, ylabel={Absolute Parameter Error}, grid=both, every major grid/.style={gray, opacity=0.5}]{%
\addplot [only marks,mark=*,red,mark options={scale=.65}]table[x index=0,y index=1,col sep=comma, green, only marks] {data.csv};\label{plots:plot1}
\addplot [only marks,mark=*,blue,mark options={scale=.65}]table[x index=0,y index=2,col sep=comma, green, only marks] {data.csv};}\label{plots:plot2}
\coordinate (top) at (rel axis cs:0,1);

\nextgroupplot[title = {\emph{(b) Plot 2}}, xlabel={$Number~of~Recursions$}, ylabel={Absolute Parameter Error}, grid=both, every major grid/.style={gray, opacity=0.5}]{%                  
\addplot [only marks,mark=*,red,mark options={scale=.65}]table[x index=0,y index=1,col sep=comma, green, only marks] {data.csv};
\addlegendentry{$\delta_{r2}$}
\addplot [only marks,mark=*,blue,mark options={scale=.65}]table[x index=0,y index=2,col sep=comma, green, only marks] {data.csv};
\addlegendentry{$\delta_{r2}$}}

\nextgroupplot[title = {\emph{(b) Plot 3}}, xlabel={$Number~of~Recursions$}, ylabel={Absolute Parameter Error}, grid=both, every major grid/.style={gray, opacity=0.5}]{%
\addplot [only marks,mark=*,cyan,mark options={scale=.65}] table[x index=0,y index=2,col sep=comma] {data.csv};
\addlegendentry{$\delta_{r3}$}}

\nextgroupplot[title = {\emph{(b) Plot 4}}, xlabel={$Number~of~Recursions$}, ylabel={Absolute Parameter Error}, grid=both, every major grid/.style={gray, opacity=0.5}]{%
\addplot [only marks,mark=*,cyan,mark options={scale=.65}] table[x index=0,y index=3,col sep=comma] {data.csv};
\addlegendentry{$\delta_{r3}$}}

\nextgroupplot[title = {\emph{(b) Plot 5}}, xlabel={$Number~of~Recursions$}, ylabel={Absolute Parameter Error}, grid=both, every major grid/.style={gray, opacity=0.5}]{%
\addplot [only marks,mark=*,cyan,mark options={scale=.65}] table[x index=0,y index=4,col sep=comma] {data.csv};
\addlegendentry{$\delta_{r3}$}}

\nextgroupplot[title = {\emph{(b) Plot 6}}, xlabel={$Number~of~Recursions$}, ylabel={Absolute Parameter Error}, grid=both, every major grid/.style={gray, opacity=0.5}]{%
\addplot [only marks,mark=*,cyan,mark options={scale=.65}] table[x index=0,y index=1,col sep=comma] {data.csv};
\addlegendentry{$\delta_{r3}$}}

\coordinate (bot) at (rel axis cs:1,0);
\end{groupplot}

%\path (top-|current bounding box.west)--
%          node[anchor=south,rotate=90] {throughput}
%          (bot-|current bounding box.west);
%% legend
%\path (top|-current bounding box.north)--
%      coordinate(legendpos)
%      (bot|-current bounding box.north);
%\matrix[
%    matrix of nodes,
%    anchor=south,
%    draw,
%    inner sep=0.2em,
%    draw
%  ]at([yshift=1ex]legendpos)
%  {
%    \ref{plots:plot1}& curve 1&[5pt]
%    \ref{plots:plot2}& curve2&[5pt]};
\end{tikzpicture}
\caption[Plot showing (a) position ${\mathbf{P_{T}}}$ and (b)the Parameters Absolute Errors. ]{Plot showing (a) position ${\mathbf{P_{T}}}$ and (b) the Parameters Absolute Errors.}\label{abserror}
\end{figure}

\end{document} 

答案1

首先,用于生成图例的\\末尾缺少一个 。我删除了所有的s。请注意,您不需要在 中的 s周围加上一对括号,因此我也删除了后面的括号。matrix\addlegendentry\addplotgroupplot\nextgroupplot[]

我还稍微缩小了尺寸(这是必要的,以便使其与图例一起放在一页中),并从xlabels 中删除了数学模式。不要将其$...$用于斜体。

\documentclass[11pt]{book}
\usepackage[top=3cm,bottom=3cm,left=3.2cm,right=3.2cm,headsep=10pt,a4paper]{geometry}
\usepackage{graphicx}
\usepackage{tikz}
\usepackage{float}
\usepackage{pgfplots,pgfplotstable, booktabs}
\usetikzlibrary{pgfplots.groupplots, matrix}
\pgfplotsset{compat=1.8}

\begin{filecontents*}{data.csv}
  Iter   x1   x2      x3      x4      x5      x6
     1, 0.9317521, 1.1849326, 1.6130556, 0.8022207, 0.9317521, 1.1849326
     2, 1.8946202, 1.1228282, 1.8964566, -0.5353802, 1.8946202, 1.1228282
     3, 1.5243302, 1.0372991, 1.4375012, 0.9719003, 1.5243302, 1.0372991
     4, 1.5797030, 1.1346832, 1.8717142, 0.3138737, 1.5797030, 1.1346832
     5, 1.8814457, 1.0529187, 2.0568468, -0.5509391, 1.8814457, 1.0529187
     6, 2.0435003, 1.0470546, 2.0621956, -0.3565483, 2.0435003, 1.0470546
     7, 2.0373926, 1.1215579, 2.1836100, 0.3360301, 2.0373926, 1.1215579
     8, 1.9797077, 1.1632352, 1.8299063, 0.3871091, 1.9797077, 1.1632352
     9, 1.9972528, 1.1952478, 1.8133509, -0.0867033, 1.9972528, 1.1952478
    10, 1.8320176, 1.0625633, 1.0727495, 1.7256738, 1.8320176, 1.0625633 
\end{filecontents*}

\pgfplotsset{minor grid style={dashed,red}}
\pgfplotsset{major grid style={dotted,green!50!black}}

\begin{document}
    \begin{figure}[H]
        \centering
        \begin{tikzpicture}
            \begin{groupplot}[group style={
                              group size=2 by 3,
                              vertical sep=2cm,
                              horizontal sep = 2cm,
                              ylabels at=edge left},
                              width=7cm,
                              height=6cm,
                              try min ticks=5,
                              ]
\nextgroupplot[title = {\emph{(b) Plot 1}}, xlabel={Number of Recursions}, ylabel={Absolute Parameter Error}, grid=both, every major grid/.style={gray, opacity=0.5}]

\addplot [only marks,mark=*,red,mark options={scale=.65}]table[x index=0,y index=1,col sep=comma, green, only marks] {data.csv};\label{plots:plot1}
\addplot [only marks,mark=*,blue,mark options={scale=.65}]table[x index=0,y index=2,col sep=comma, green, only marks] {data.csv};\label{plots:plot2}
\coordinate (top) at (rel axis cs:0,1);

\nextgroupplot[title = {\emph{(b) Plot 2}}, xlabel={Number of Recursions}, ylabel={Absolute Parameter Error}, grid=both, every major grid/.style={gray, opacity=0.5}]              
\addplot [only marks,mark=*,red,mark options={scale=.65}]table[x index=0,y index=1,col sep=comma, green, only marks] {data.csv};\label{plots:plot3}
\addplot [only marks,mark=*,blue,mark options={scale=.65}]table[x index=0,y index=2,col sep=comma, green, only marks] {data.csv};


\nextgroupplot[title = {\emph{(b) Plot 3}}, xlabel={Number of Recursions}, ylabel={Absolute Parameter Error}, grid=both, every major grid/.style={gray, opacity=0.5}]
\addplot [only marks,mark=*,cyan,mark options={scale=.65}] table[x index=0,y index=2,col sep=comma] {data.csv};


\nextgroupplot[title = {\emph{(b) Plot 4}}, xlabel={Number of Recursions}, ylabel={Absolute Parameter Error}, grid=both, every major grid/.style={gray, opacity=0.5}]
\addplot [only marks,mark=*,cyan,mark options={scale=.65}] table[x index=0,y index=3,col sep=comma] {data.csv};


\nextgroupplot[title = {\emph{(b) Plot 5}}, xlabel={Number of Recursions}, ylabel={Absolute Parameter Error}, grid=both, every major grid/.style={gray, opacity=0.5}]
\addplot [only marks,mark=*,cyan,mark options={scale=.65}] table[x index=0,y index=4,col sep=comma] {data.csv};


\nextgroupplot[title = {\emph{(b) Plot 6}}, xlabel={Number of Recursions}, ylabel={Absolute Parameter Error}, grid=both, every major grid/.style={gray, opacity=0.5}]
\addplot [only marks,mark=*,cyan,mark options={scale=.65}] table[x index=0,y index=1,col sep=comma] {data.csv};


\coordinate (bot) at (rel axis cs:1,0);
\end{groupplot}

\path (top-|current bounding box.west)--
          node[anchor=south,rotate=90] {throughput}
          (bot-|current bounding box.west);
% legend
\path (top|-current bounding box.north)--
      coordinate(legendpos)
      (bot|-current bounding box.north);
\matrix[
    matrix of nodes,
    anchor=south,
    draw,
    inner sep=0.2em,
    draw
  ]at([yshift=1ex]legendpos)
  {
    \ref{plots:plot1}& curve 1&[5pt]
    \ref{plots:plot2}& curve2&[5pt] 
    \ref{plots:plot3}& curve3\\};
\end{tikzpicture}
\caption[Plot showing (a) position ${\mathbf{P_{T}}}$ and (b)the Parameters Absolute Errors. ]{Plot showing (a) position ${\mathbf{P_{T}}}$ and (b) the Parameters Absolute Errors.}\label{abserror}
\end{figure}

\end{document} 

相关内容