4

This is a follow up to How to add another row of tick labels? I currently have another row of tick labels for every plot in a group of plots. What I want to do is to have the descriptions for the extra row of tick labels to appear only once per row of plots, like what y descriptions at=edge left does for axis descriptions.

What I currently get is the following where the undesirable descriptions are marked:

My failed attempt of having the extra row of tick labels appear only once per row of plots

\documentclass{article}

\usepackage{pgfplots}

\usepgfplotslibrary{groupplots}

\pgfplotsset{
  domain=0:1,
  xmin=0, xmax=1,
  ymax=1, ymin=-1
}

\begin{document}

\begin{figure}
  \begin{tikzpicture}
    \begin{groupplot}[width=6cm,
      clip=false,
      xtick align=inside,
      extra x ticks={0, 0.2, 0.4, ..., 1},
      every extra x tick/.style={major tick length=0pt,
        xtick align=outside, yshift=-10pt},
      group style={group size=2 by 2,
        y descriptions at=edge left},
      ylabel={\(f(x)\)},
      ]
      \pgfplotsinvokeforeach{0.2, 0.4, 0.6, 0.8}{
        \nextgroupplot[extra x tick labels={#1, #1, #1, #1, #1 ,#1}]
        \addplot{#1 - x};
        \node  at (axis description cs:0,0) [anchor=north east, align=left,xshift=-12pt] {\(x_1\)};
        \node  at (axis description cs:0,0) [anchor=north east, align=left,xshift=-12pt,yshift=-11pt] {\(x_2\)};
      }
    \end{groupplot}
  \end{tikzpicture}
\end{figure}

\end{document}

Obviously I could skip the loop and write out each plot and their options explicitly. But that seems like a hack and bad style. For instance, it would mean that content and style are less separated. Ideally I would like to keep the loop but still have control over the style options, such as via an interface that lets you do things like extra x descriptions at=edge left.

1 Answer 1

2

The current column of the groupplots grid is stored in the \pgfplots@group@current@column counter. You can use this to check whether we're in the first column and only print the extra labels if we are.

This approach can also be used to add a legend only to the first plot, by using the \pgfplots@group@current@plot counter, which contains the plot index.

\documentclass{article}

\usepackage{pgfplots}

\makeatletter
\newcommand{\currentcolumn}{\the\pgfplots@group@current@column}
\newcommand{\currentplot}{\the\pgfplots@group@current@plot}
\makeatother


\usepgfplotslibrary{groupplots}

\pgfplotsset{
  domain=0:1,
  xmin=0, xmax=1,
  ymax=1, ymin=-1
}

\begin{document}

\begin{figure}
  \begin{tikzpicture}
    \begin{groupplot}[width=6cm,
      clip=false,
      xtick align=inside,
      extra x ticks={0, 0.2, 0.4, ..., 1},
      every extra x tick/.style={major tick length=0pt,
        xtick align=outside, yshift=-10pt},
      group style={group size=2 by 2,
        y descriptions at=edge left},
      ylabel={\(f(x)\)},
      ]
      \pgfplotsinvokeforeach{0.2, 0.4, 0.6, 0.8}{
        \nextgroupplot[extra x tick labels={#1, #1, #1, #1, #1 ,#1}]
        \addplot{#1 - x};
        \ifnum\currentcolumn=1
            \node  at (axis description cs:0,0) [anchor=north east, align=left,xshift=-12pt] {\(x_1\)};
                \node  at (axis description cs:0,0) [anchor=north east, align=left,xshift=-12pt,yshift=-11pt] {\(x_2\)};
        \fi
        \ifnum\currentplot=1
        \pgfplotsset{legend entries=Boring Data}
        \fi
      }
    \end{groupplot}
  \end{tikzpicture}
\end{figure}

\end{document}
2
  • Thanks! Is there a way to check if one is at the first column of the first row so that one may send a legend to name=grouplegend to that \nextgroupplot? That is the reason I asked tex.stackexchange.com/q/66069/5701 but that may be the wrong approach to add a legend to the first plot and still have a loop.
    – N.N.
    Aug 8, 2012 at 10:54
  • 1
    @N.N.: Sure! The current plot number is also available (as are the total number of rows and columns), so you can use that to check whether you're in the first plot and then conditionally call \pgfplotsset{legend entries=...}. I've edited my answer to show an example.
    – Jake
    Aug 8, 2012 at 11:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .