多图 pgfplots 代码无法正常工作

多图 pgfplots 代码无法正常工作

我一直在尝试使用我的代码来获得这种外观:

在此处输入图片描述

到目前为止,我一直很为难。我将组样式设置为 3 x 3,但似乎这可能是问题所在。请帮我获得上述外观。谢谢!

这是我的代码:

\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=3 by 3,
                              vertical sep=2cm,
                              horizontal sep = 2cm,
                              ylabels at=edge left},
                              width=7cm,
                              height=6cm,
                              try min ticks=5,
                              ]
\nextgroupplot[title = {\emph{(a) Plot 1}}, xlabel={Number of Recursions}, 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}, 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}, 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}, 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}, 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}, 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:2,0);
\end{groupplot}

\path (top-|current bounding box.west)--
          node[anchor=south,rotate=90] {\large{\bf{Absolute Parameter Error $\mid$Estimated - Actual$\mid$}}}
          (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

您可以使用两个groupplot环境来完成此操作。由于数据实际上与当前问题无关,因此下面的示例仅使用\addplot{rnd};

在此处输入图片描述

\documentclass[11pt]{book}
\usepackage[top=3cm,bottom=3cm,left=3.2cm,right=3.2cm,headsep=10pt,a4paper]{geometry}
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}
\usetikzlibrary{calc}

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

\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}
\begin{groupplot}[
   group style={
     group size=3 by 1,
     group name=g1,
     horizontal sep=1.7cm},
   width=7cm/3*2]
\nextgroupplot[title = {\emph{(a) Plot 1}}, xlabel={Number of Recursions}, grid=both, every major grid/.style={gray, opacity=0.5}]

\addplot{rnd};

\nextgroupplot[title = {\emph{(b) Plot 2}}, xlabel={Number of Recursions}, grid=both, every major grid/.style={gray, opacity=0.5}]

\addplot{rnd};

\nextgroupplot[title = {\emph{(b) Plot 3}}, xlabel={Number of Recursions}, grid=both, every major grid/.style={gray, opacity=0.5}]
\addplot{rnd};
\end{groupplot}

\coordinate (nextgroup) at ($(g1 c1r1.south west)+(0,-2.5cm)$);

\begin{groupplot}[anchor=outer north west,
                              group style={group name=g2,
                                group size=2 by 2,
                                vertical sep=2cm,
                                horizontal sep = 2cm,
                                 ylabels at=edge left},
                              width=7cm,
                              height=6cm,
                              try min ticks=5,
                              ]
\nextgroupplot[at={(nextgroup)},anchor=north west,title = {\emph{(b) Plot 4}}, xlabel={Number of Recursions}, grid=both, every major grid/.style={gray, opacity=0.5}]
\addplot{rnd};

\nextgroupplot[title = {\emph{(b) Plot 5}}, xlabel={Number of Recursions}, grid=both, every major grid/.style={gray, opacity=0.5}]
\addplot{rnd};

\nextgroupplot[title = {\emph{(b) Plot 6}}, xlabel={Number of Recursions}, grid=both, every major grid/.style={gray, opacity=0.5}]
\addplot{rnd};

\nextgroupplot[title = {\emph{(b) Plot 7}}, xlabel={Number of Recursions}, grid=both, every major grid/.style={gray, opacity=0.5}]
\addplot{rnd};
\end{groupplot}


\node [rotate=90] at ($(g2 c1r1.south west)!0.5!(g2 c1r2.north west) + (-1.2cm,0)$) {Absolute errors};
\node [rotate=90] at ($(g2 c2r1.south west)!0.5!(g2 c2r2.north west) + (-1.2cm,0)$) {Relative errors};

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

相关内容