# pgfplots - How can I change an option (e.g. xlabel) for all subsequent groupplots?

If xlabel or ylabel are defined in the groupplots options, the values are used for all subsequent plots. Same with other options.

Can one change the xlabel for all following groupplots from one point on, without having to repeat the option for each groupplot? This comes in especially handy when one wants to define the values for ymin and ymax for each row in a groupplots-environment.

## MWE

\documentclass[
a4paper
]{scrartcl}

\usepackage{
lmodern,
tikz,
pgfplots,
}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

\usepgfplotslibrary{groupplots}

\begin{document}
\begin{center}
\begin{tikzpicture}[font=\footnotesize]
\begin{groupplot}[
group style={
group size=2 by 2,
horizontal sep=0.2cm,
vertical sep=1.5cm,
ylabels at=edge left,
yticklabels at=edge left,
},
%
ymin=300,
ymax=1300,
%
xlabel={The label for the x-axis},
ylabel={Some y-values},
%
scale ticks above exponent={2},
]
\nextgroupplot
\nextgroupplot
\nextgroupplot[xlabel={Now I define another xlabel2}]
\nextgroupplot[xlabel={And here I have to repeat it otherwise the label for the first plots gets put here}, xlabel style={text width=5cm}]
\end{groupplot}
\end{tikzpicture}
\end{center}
\end{document}

• Does \pgfplotsset{xlabel={label}} work? – darthbith Sep 17 '14 at 20:57
• @darthbith No, only works for one plot when placed directly in front of it, otherwise it breaks the document. – henry Sep 17 '14 at 21:08
• For xlabel you can use the \globaldefs trick from tex.stackexchange.com/a/15263/586, but that doesn't appear to work for axis limits. – Torbjørn T. Sep 22 '14 at 14:35

If you don't mind augmenting one internal macro of the groupplots library a little bit, you can define a new sticky options style that you can use in the optional argument to \nextgroupplot[...] to set options that will stay active for the remainder of the groupplots environment (or until you call sticky options again).

Putting the following code chunk into your preamble will make the sticky options available.

\makeatletter
\pgfplotsset{
sticky options/.store in=\pgfplots@group@stickyoptions,
sticky options={}
}

\def\pgfplots@group@nextplot[#1]{%
% Check if it legal to produce another plot
\ifnum\pgfplots@group@current@plot=\pgfplots@group@totalplots\relax
\pgfplotswarning{groupplots/too many plots}{\the\pgfplots@group@current@plot}{\pgfplots@group@totalplots}\pgfeov
\else
%
% If there has been other plots before then end the plot
%
\ifnum0<\pgfplots@group@current@plot\relax
% DEBUG: \message{DEBUG: Ending plot}
\endpgfplots@environment@opt
\fi

% Use a group so we can discard all the options that aren't sticky
\begingroup
% Execute the keys
\pgfqkeys{/pgfplots}{#1}
% Smuggle the contents of the sticky options key out of the group
\pgfmath@smuggleone\pgfplots@group@stickyoptions
% End the group
\endgroup
% Prepare for next plot, increment plot, row and column
\pgfplots@group@increment@numbers
% DEBUG: \message{DEBUG: Cur. Plot: \the\pgfplots@group@current@plot. Current Row: \the\pgfplots@group@current@row. Current Column: \the\pgfplots@group@current@column.}
% Creation of the new axis environment
\pgfplots@group@environment@create{#1, \pgfplots@group@stickyoptions}
\fi
}
\makeatother


\documentclass[border=5mm]{standalone}

\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}

\makeatletter
\pgfplotsset{
sticky options/.store in=\pgfplots@group@stickyoptions,
sticky options={}
}

\def\pgfplots@group@nextplot[#1]{%
% Check if it legal to produce another plot
\ifnum\pgfplots@group@current@plot=\pgfplots@group@totalplots\relax
\pgfplotswarning{groupplots/too many plots}{\the\pgfplots@group@current@plot}{\pgfplots@group@totalplots}\pgfeov
\else
%
% If there has been other plots before then end the plot
%
\ifnum0<\pgfplots@group@current@plot\relax
% DEBUG: \message{DEBUG: Ending plot}
\endpgfplots@environment@opt
\fi

% Use a group so we can discard all the options that aren't sticky
\begingroup
% Execute the keys
\pgfqkeys{/pgfplots}{#1}
% Smuggle the contents of the sticky options key out of the group
\pgfmath@smuggleone\pgfplots@group@stickyoptions
% End the group
\endgroup
% Prepare for next plot, increment plot, row and column
\pgfplots@group@increment@numbers
% DEBUG: \message{DEBUG: Cur. Plot: \the\pgfplots@group@current@plot. Current Row: \the\pgfplots@group@current@row. Current Column: \the\pgfplots@group@current@column.}
% Creation of the new axis environment
\pgfplots@group@environment@create{#1, \pgfplots@group@stickyoptions}
\fi
}
\makeatother

\begin{document}
\begin{tikzpicture}[font=\footnotesize]
\begin{groupplot}[
group style={
group size=2 by 2,
horizontal sep=0.2cm,
vertical sep=1.5cm,
ylabels at=edge left,
yticklabels at=edge left,
},
%
ymin=300,
ymax=1300,
%
xlabel={The label for the x-axis},
ylabel={Some y-values},
%
scale ticks above exponent={2},
]
\nextgroupplot
\nextgroupplot
\nextgroupplot[sticky options={xlabel={Now I define another xlabel2}, ymin=0}]
\nextgroupplot
\end{groupplot}
\end{tikzpicture}

\end{document}


If taking the small detour of saving the x-label in a macro is OK, you can do something like this:

\documentclass[
a4paper
]{scrartcl}

\usepackage{
lmodern,
tikz,
pgfplots,
}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

\usepgfplotslibrary{groupplots}

\newcommand\somexlabel{Label for $x$-axis}

\begin{document}
\begin{center}
\begin{tikzpicture}[font=\footnotesize]
\begin{groupplot}[
group style={
group size=2 by 2,
horizontal sep=0.2cm,
vertical sep=1.5cm,
ylabels at=edge left,
yticklabels at=edge left,
},
%
ymin=300,
ymax=1300,
%
xlabel={\somexlabel},
ylabel={Some y-values},
%
scale ticks above exponent={2},
width=6.5cm
]
\nextgroupplot
\nextgroupplot
\nextgroupplot
\xdef\somexlabel{New fancy label}

• Hm... I don't get how I could get it to work for multiple groupplots (as in, multiple figures) in a document then? You mean one should define as many xlabelDESCRIPTION as needed then? – henry Sep 21 '14 at 18:17