[Tex/LaTex] pfgplots groupplot with more than one \addplot per \nextgroupplot

csvgroupplotspgfplotstikz-pgf

I'm having trouble making a group plot in pgfplots. I want to have 2 separate graphs, each with two regression lines and two sets of dots, but groupplot lumps everything onto 1 set of axes. I can't an find example of having more than one addplot command per nextgroupplot command anywhere on the web, so I'm stumped. My page is as follows (the formatting is a bit off because it's automatically generated):

\documentclass{article}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepgfplotslibrary{groupplots}
\usepackage{siunitx}
\usepackage{graphicx}
\pgfplotsset{compat=1.5}
\title{Test Report}
\author{Big Joe}
\date{}

\begin{document}

\maketitle\begin{tikzpicture}\begin{groupplot}\nextgroupplot [xlabel=Order of spot,
                                  ylabel=$sin(\theta) \cdot d $ /m,
                                  legend pos=outer north east,
                                  title=Given wavelength: 460]
                    \addplot
                         [only marks,
                          mark=*,
                          mark options={red}]
                    table
                          [y={Min dSin},
                           x=Order,
                           col sep=comma]
                    {460photocell.csv};
                    \addplot
                         [only marks,
                          mark=*,
                          mark options={orange}]
                    table
                         [y={Max dSin},
                          x=Order,
                          col sep=comma]
                     {460photocell.csv};
                     \addplot[thin, blue] table [
                          col sep=comma,
                          x=Order,
                          y={create col/linear regression={y={Max dSin}, x=Order}}]
                     {460photocell.csv};
                       \xdef\slopeA{\pgfplotstableregressiona}
                       \xdef\interceptA{\pgfplotstableregressionb}
                     \addplot [thin, green] table [
                          col sep=comma,
                          x=Order,
                          y={create col/linear regression={y={Min dSin}, x=Order}}]
                     {460photocell.csv};
                       \xdef\slopeB{\pgfplotstableregressiona}
                       \xdef\interceptB{\pgfplotstableregressionb}
                     \addlegendentry{Max Slope}
                     \addlegendentry{Min Slope}
                     \addlegendentry{
                          $ \pgfmathprintnumber{\slopeA} \cdot x \pgfmathprintnumber[print sign]{\interceptA} $ }
                     \addlegendentry{
                          $ \pgfmathprintnumber{\slopeB} \cdot x \pgfmathprintnumber[print sign]{\interceptB} $ }\nextgroupplot [xlabel=Order of spot,
                                  ylabel=$sin(\theta) \cdot d $ /m,
                                  legend pos=outer north east,
                                  title=Given wavelength: 470]
                    \addplot
                         [only marks,
                          mark=*,
                          mark options={red}]
                    table
                          [y={Min dSin},
                           x=Order,
                           col sep=comma]
                    {470photocell.csv};
                    \addplot
                         [only marks,
                          mark=*,
                          mark options={orange}]
                    table
                         [y={Max dSin},
                          x=Order,
                          col sep=comma]
                     {470photocell.csv};
                     \addplot[thin, blue] table [
                          col sep=comma,
                          x=Order,
                          y={create col/linear regression={y={Max dSin}, x=Order}}]
                     {470photocell.csv};
                       \xdef\slopeA{\pgfplotstableregressiona}
                       \xdef\interceptA{\pgfplotstableregressionb}
                     \addplot [thin, green] table [
                          col sep=comma,
                          x=Order,
                          y={create col/linear regression={y={Min dSin}, x=Order}}]
                     {470photocell.csv};
                       \xdef\slopeB{\pgfplotstableregressiona}
                       \xdef\interceptB{\pgfplotstableregressionb}
                     \addlegendentry{Max Slope}
                     \addlegendentry{Min Slope}
                     \addlegendentry{
                          $ \pgfmathprintnumber{\slopeA} \cdot x \pgfmathprintnumber[print sign]{\interceptA} $ }
                     \addlegendentry{
                          $ \pgfmathprintnumber{\slopeB} \cdot x \pgfmathprintnumber[print sign]{\interceptB} $ }\end{groupplot} \end{tikzpicture}\end{document}

where 460photocell.csv contains:

Order,Min dSin,Max dSin
1,0,0
2,4.51232E-07,5.96007E-07
3,8.95839E-07,1.07146E-06
4,1.34728E-06,1.51745E-06

and 470photocell.csv contains:

Order,Min dSin,Max dSin
1,0,0
2,4.64486E-07,5.78254E-07
3,9.60654E-07,1.08358E-06
4,1.50085E-06,1.58593E-06

Is this even possible?

Best Answer

You have to set the total number of plots that will be in the groupplot environment using either group size=1 by 2 or columns=1 and rows=2. Note, that the initially value is group size=1 by 1 that means columns=1 androws=1`.

So you could use

  \begin{groupplot}[
      group style={group size=1 by 2, vertical sep=2cm},
      xlabel=Order of spot,
      ylabel=$sin(\theta) \cdot d $ /m,
      legend pos=outer north east
    ]
    \nextgroupplot [title=Given wavelength: 460]
      ...
    \nextgroupplot [title=Given wavelength: 470]
      ...
  \end{groupplot}

enter image description here

Code:

\documentclass{article}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepgfplotslibrary{groupplots}
\pgfplotsset{compat=1.5}% Why 1.5? The current version is 1.11 or 1.12

\begin{filecontents*}{460photocell.csv}
Order,Min dSin,Max dSin
1,0,0
2,4.51232E-07,5.96007E-07
3,8.95839E-07,1.07146E-06
4,1.34728E-06,1.51745E-06
\end{filecontents*}
\begin{filecontents*}{470photocell.csv}
Order,Min dSin,Max dSin
1,0,0
2,4.64486E-07,5.78254E-07
3,9.60654E-07,1.08358E-06
4,1.50085E-06,1.58593E-06
\end{filecontents*}

\begin{document}
\begin{tikzpicture}
  \begin{groupplot}[
      group style={group size=1 by 2, vertical sep=2cm},
      xlabel=Order of spot,
      ylabel=$sin(\theta) \cdot d $ /m,
      legend pos=outer north east
    ]
    \nextgroupplot [title=Given wavelength: 460]
      \addplot[only marks,mark=*,mark options={red}]
        table[y={Min dSin},x=Order,col sep=comma]{460photocell.csv};
      \addplot[only marks,mark=*,mark options={orange}]
        table[y={Max dSin},x=Order,col sep=comma]{460photocell.csv};
      \addplot[thin, blue]
          table [col sep=comma,x=Order,y={create col/linear regression={y={Max dSin}}}]
            {460photocell.csv};
        \xdef\slopeA{\pgfplotstableregressiona}
        \xdef\interceptA{\pgfplotstableregressionb}
      \addplot [thin, green]
        table [col sep=comma,x=Order,y={create col/linear regression={y={Min dSin},}}]
            {460photocell.csv};
        \xdef\slopeB{\pgfplotstableregressiona}
        \xdef\interceptB{\pgfplotstableregressionb}
        \addlegendentry{Max Slope}
        \addlegendentry{Min Slope}
        \addlegendentry{$\pgfmathprintnumber{\slopeA}\cdot x\pgfmathprintnumber[print sign]{\interceptA}$}
        \addlegendentry{$\pgfmathprintnumber{\slopeB}\cdot x\pgfmathprintnumber[print sign]{\interceptB}$}
    \nextgroupplot [title=Given wavelength: 470]
      \addplot[only marks,mark=*,mark options={red}]
        table[y={Min dSin},x=Order,col sep=comma]{470photocell.csv};
      \addplot[only marks,mark=*,mark options={orange}]
        table[y={Max dSin},x=Order,col sep=comma]{470photocell.csv};
      \addplot[thin, blue]
          table [col sep=comma,x=Order,y={create col/linear regression={y={Max dSin}}}]
            {470photocell.csv};
        \xdef\slopeA{\pgfplotstableregressiona}
        \xdef\interceptA{\pgfplotstableregressionb}
      \addplot [thin, green]
        table [col sep=comma,x=Order,y={create col/linear regression={y={Min dSin},}}]
            {470photocell.csv};
        \xdef\slopeB{\pgfplotstableregressiona}
        \xdef\interceptB{\pgfplotstableregressionb}
        \addlegendentry{Max Slope}
        \addlegendentry{Min Slope}
        \addlegendentry{$\pgfmathprintnumber{\slopeA}\cdot x\pgfmathprintnumber[print sign]{\interceptA}$}
        \addlegendentry{$\pgfmathprintnumber{\slopeB}\cdot x\pgfmathprintnumber[print sign]{\interceptB}$}
  \end{groupplot} 
\end{tikzpicture}
\end{document}
Related Question