5

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.

Picture

enter image description here

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
\addplot+ coordinates{(2200,1200) (3100,500)};
\nextgroupplot
\addplot+ coordinates{(2300,1200) (4000,400)};
\nextgroupplot[xlabel={Now I define another xlabel2}]
\addplot+ coordinates{(2400,1200) (4500,400)};
\nextgroupplot[xlabel={And here I have to repeat it otherwise the label for the first plots gets put here}, xlabel style={text width=5cm}]
\addplot+ coordinates{(2500,1200) (4600,400)};
\end{groupplot}
\end{tikzpicture}
\end{center}
\end{document}
henry
  • 6,594

2 Answers2

6

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
\addplot+ coordinates{(2200,1200) (3100,500)};
\nextgroupplot
\addplot+ coordinates{(2300,1200) (4000,400)};
\nextgroupplot[sticky options={xlabel={Now I define another xlabel2}, ymin=0}]
\addplot+ coordinates{(2400,1200) (4500,400)};
\nextgroupplot
\addplot+ coordinates{(2500,1200) (4600,400)};
\end{groupplot}
\end{tikzpicture}


\end{document}
Jake
  • 232,450
1

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
\addplot+ coordinates{(2200,1200) (3100,500)};
\nextgroupplot
\addplot+ coordinates{(2300,1200) (4000,400)};
\nextgroupplot
\xdef\somexlabel{New fancy label}
\addplot+ coordinates{(2400,1200) (4500,400)};
\nextgroupplot
\addplot+ coordinates{(2500,1200) (4600,400)};

\end{groupplot}
\end{tikzpicture}
\end{center}
\end{document}
Torbjørn T.
  • 206,688
  • This is not what I meant. I edited the op to clarify what I was looking for. :) – henry Sep 21 '14 at 18:12
  • @henry I don't see how my answer doesn't do what you want, although it doesn't do it the way you suggest in your MWE. – Torbjørn T. Sep 21 '14 at 18:14
  • 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
  • @henry Well, yes. That is a (potentially significant) drawback, I know. If it is useless for your case, I can just delete my answer. – Torbjørn T. Sep 21 '14 at 18:20
  • I thought quite a bit about it and truly think it is useless because it is not an improvment, really. I suppose I am looking for a "switch"-like command/method to get there. – henry Sep 21 '14 at 19:07
  • @henry Fair enough, I'll delete in a bit. I think I considered adding the answer earlier but was unsure of whether it was useful or not. By the way, on second thought you don't need more than one macro, just do \renewcommand\somexlabel{...} before the tikzpicture, and add the \xdef within the groupplot, if needed. – Torbjørn T. Sep 21 '14 at 19:28
  • Fwiw, you brought up the topic of deletion, I think it could/should stay. :) – henry Sep 21 '14 at 20:12