Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Is there a way to automatically label a set of groupplots from the pgfplots package (A), (B), (C), ...?

\documentclass[margin=2pt]{standalone}

\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}
\pgfplotsset{compat=1.8}

\begin{document}
\begin{tikzpicture}[shorten >=4pt,shorten <=4pt]
  \begin{groupplot}[group style={group size=2 by 2,vertical sep=15mm},
    height=3.5cm,width=3.5cm,/tikz/font=\small]
    \nextgroupplot[title=(A)]
    \addplot coordinates {(0,1) (1,0)};
    \nextgroupplot[title=(B)]
    \addplot coordinates {(0,1) (1,0)};
    \nextgroupplot[title=(C)] 
    \addplot coordinates {(0,1) (1,0)};
    \nextgroupplot[title=(D)] 
    \addplot coordinates {(0,1) (1,0)};
  \end{groupplot}
\end{tikzpicture}
\end{document}

enter image description here

share|improve this question

2 Answers 2

up vote 9 down vote accepted

The groupplots library keeps the value of the current plot in the \pgfplots@group@current@plot count. So you can create the titles automatically using the alphalph package and the following style:

\makeatletter
\pgfplotsset{
    auto title/.style={
        title=(\AlphAlph{\pgfplots@group@current@plot})
    }
}
\makeatother

Then you just add auto title to the axis options (or to the \nextgroupplot options), and you'll get the desired result:

\documentclass[margin=2pt]{standalone}

\usepackage{alphalph}
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}
\pgfplotsset{compat=1.8}

\makeatletter
\pgfplotsset{
    auto title/.style={
        title=(\AlphAlph{\pgfplots@group@current@plot})
    }
}
\makeatother

\begin{document}
\begin{tikzpicture}[shorten >=4pt,shorten <=4pt]
  \begin{groupplot}[group style={group size=2 by 2,vertical sep=15mm},
    height=3.5cm,width=3.5cm,/tikz/font=\small, auto title]
    \nextgroupplot
    \addplot coordinates {(0,1) (1,0)};
    \nextgroupplot
    \addplot coordinates {(0,1) (1,0)};
    \nextgroupplot
    \addplot coordinates {(0,1) (1,0)};
    \nextgroupplot
    \addplot coordinates {(0,1) (1,0)};
  \end{groupplot}
\end{tikzpicture}
\end{document}
share|improve this answer

There is still something I'm not quite solving properly, though this gives the desired result. As I would have wanted it to work, you would use \startaddplot for your first plot and \nextaddplot for subsequent plots.

But what happened is that the counter I used is somehow lagging in its step. So the crass fix I had to apply was initializing the counter to -1 instead of the expected value of 0, and also require you to issue a \noaddplot when you are done, to retroactively step the counter for the final plot.

I'm sure someone like egreg will chime in to tell me why my counter isn't keeping up.

\documentclass[margin=2pt]{standalone}

\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}
\pgfplotsset{compat=1.8}
\newcounter{plotno}
\newcommand\startaddplot{%
  \global\setcounter{plotno}{-1}%
  \nextaddplot%
}
\newcommand\nextaddplot{
  \global\stepcounter{plotno}%
  \nextgroupplot[title=(\Alph{plotno})]%
  \addplot%
}
\def\noaddplot{\global\stepcounter{plotno}}
\begin{document}
\begin{tikzpicture}[shorten >=4pt,shorten <=4pt]
  \begin{groupplot}[group style={group size=2 by 2,vertical sep=15mm},
    height=3.5cm,width=3.5cm,/tikz/font=\small]
    \startaddplot coordinates {(0,1) (1,0)};
    \nextaddplot coordinates {(0,1) (1,0)};
    \nextaddplot coordinates {(0,1) (1,0)};
    \nextaddplot coordinates {(0,1) (1,0)};
    \noaddplot
  \end{groupplot}
\end{tikzpicture}
\end{document}

enter image description here

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.