[Tex/LaTex] How to set axes limits for two types of plots when using groupplots

groupplotspgfplots

I have a rather big figure with two rows of plots where the first row has one y-axis and the second row has another y-axis. I would like to set the axis limits for these two sets of y-axes without having to write \nextgroupplot[ymin=###,ymax=###] for every plot, for example using \pgfplotsset{group/every plot/.style={ymin=###,ymax=###}} but this doesn't seem to work.

Here's an example of what I want to achieve

\documentclass{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}
\begin{document}
\begin{tikzpicture}
\begin{groupplot}[group style={group size=3 by 2,every plot/.style={xmin=0,xmax=10}},width=4cm,height=6cm]

\nextgroupplot[ylabel={Quantity1 [unit1]},ymin=0,ymax=5,title={First conditions}]
\addplot {x};
\nextgroupplot[ymin=0,ymax=5,title={Second conditions}]
\addplot {x};
\nextgroupplot[ymin=0,ymax=5,title={Third conditions}]
\addplot {x};

\nextgroupplot[ylabel={Quantity2 [unit2]},ymin=100,ymax=400]
\addplot {400/x^.5};
\nextgroupplot[ymin=100,ymax=400]
\addplot {400/x^.5};
\nextgroupplot[ymin=100,ymax=400]
\addplot {400/x^.5};
\end{groupplot}
\end{tikzpicture}
\end{document}

Which gives: Desired Result

Here's the code with attempted simplifications (and correction for LaRiFaRi's comment):

\documentclass{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}
\begin{document}
\begin{tikzpicture}
\begin{groupplot}[group style={group size=3 by 2,every plot/.style={xmin=0,xmax=10}},width=4cm,height=6cm]
\pgfplotsset{group/every plot/.append style={ymin=0,ymax=5}}
\nextgroupplot[ylabel={Quantity1 [unit1]},title={First conditions}]
\addplot {x};
\nextgroupplot[title={Second conditions}]
\addplot {x};
\nextgroupplot[title={Third conditions}]
\addplot {x};

\pgfplotsset{group/every plot/.style={ymin=100,ymax=400}}
\nextgroupplot[ylabel={Quantity2 [unit2]}]
\addplot {400/x^.5};
\nextgroupplot
\addplot {400/x^.5};
\nextgroupplot
\addplot {400/x^.5};
\end{groupplot}
\end{tikzpicture}
\end{document}

But unfortunately, this messes up the axes for the second group of plots. Is there another way to set the axis limits for several plots?

Best Answer

I would say this is not a bug but a feature. Using \pgfplotsset after the first \nextgroupplot is (most probably) the same as using that inside of an axis environment and thus is scoped to that. Said that I also recommend starting a new groupplot environment. About "repeating" the options: Just create a style collecting all stuff that is in common and apply the differences to each new groupplot environment.

For more details please have a look at the comments in the code.

% used PGFPlots v1.14
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
    \usepgfplotslibrary{groupplots}
    \pgfplotsset{
        compat=1.3,
        % create a style that collects stuff that is all `\nextgroupplot`s in common
        my groupplot style/.style={
            width=4cm,
            height=6cm,
            % because of a bug
            %     <https://sourceforge.net/p/pgfplots/bugs/137/>
            % one currently cannot use `group style' in other styles
            group/group size=3 by 1,
            group/every plot/.style={xmin=0,xmax=10},
        },
    }
\begin{document}
\begin{tikzpicture}
    \begin{groupplot}[
        % apply created style here
        my groupplot style,
        % state a name for this `groupplot' to have a reference point to
        % put the second `groupplot` relative to this one
        group/group name=first group,
        ymin=0,
        ymax=5,
    ]
    \nextgroupplot[ylabel={Quantity1 [unit1]},title={First conditions}]
        \addplot {x};
    \nextgroupplot[title={Second conditions}]
        \addplot {x};
    \nextgroupplot[title={Third conditions}]
        \addplot {x};
    \end{groupplot}

    \begin{groupplot}[
        my groupplot style,
        ymin=100,
        ymax=400
    ]
    \nextgroupplot[
        ylabel={Quantity2 [unit2]},
        % state where the first plot should be plotted (and anchored)
        % (Please note that this doesn't work when these options are provided
        %  to the `groupplot' options)
        at={($ (first group c1r1.south west) - (0,10mm) $)},
        anchor=north west,
    ]
        \addplot {400/x^.5};
    \nextgroupplot
        \addplot {400/x^.5};
    \nextgroupplot
        \addplot {400/x^.5};
    \end{groupplot}
\end{tikzpicture}
\end{document}

image showing the result of above code