2

I am plotting a lower triangular matrix of plots with groupplot, and I would like to add a title to the plots on the diagonal. I generate these plots by reading data using \foreach, and therefore need to access the title strings via either a file or a list stored in pgfplots.

The title file would have a math environment in them, like the code below. Is there a way to apply these values to the plot below? For this example the three diagonal plots would have the titles {$\alpha_1$, $\alpha_2$, $beta$}.

Here is my minimal working example:

\documentclass[border=3.14pt]{standalone}
\usepackage{pgfplots,pgfplotstable}
\usepgfplotslibrary{groupplots} 
\pgfplotsset{compat=1.15}
\newcounter{irun}

\usepackage{filecontents}
\begin{filecontents}{plot_titles.txt}
$\alpha_1$
$\alpha_2$
$\beta$
$\gamma$
$\delta$
\end{filecontents}

\begin{document}
\begin{tikzpicture}
  \begin{groupplot}[group style={group size=3 by 3},
    view={0}{90},
    try min ticks=2,
    max space between ticks=50,
    width=3.5cm,
    height=3.5cm]
    \pgfplotsforeachungrouped \i/\j in { % the following table is generated by a script
    1 / 1,
    1 / 2,
    1 / 3,
    2 / 1,
    2 / 2,
    2 / 3,
    3 / 1,
    3 / 2,
    3 / 3}{\pgfmathtruncatemacro\b{int(\theirun - 1)}
          \ifnum\i=\j
            \edef\temp{\noexpand\nextgroupplot\noexpand\addplot coordinates {(0,0) (1,1) (2,2)};} % Line plot
            \temp
            \typeout{\i=\j}
          \else
            \ifnum\j>\i
                \edef\temp{\noexpand\nextgroupplot[group/empty plot]} % No plot
                \temp
                \typeout{\j>\i}
            \else
                \edef\temp{\noexpand\nextgroupplot\noexpand\addplot coordinates {(0,0) (1,2) (2,1)};} % Contour plot
                \temp
                \typeout{\j<\i}
            \fi
          \fi
    }
  \end{groupplot}
\end{tikzpicture}
\end{document}

Generated output from MWE

1
\documentclass[border=3.14pt]{standalone}
\usepackage{pgfplots,pgfplotstable}
\usepgfplotslibrary{groupplots} 
\pgfplotsset{compat=1.15}

\usepackage{filecontents}
\begin{filecontents}{plot_titles.txt}
$\alpha_1$
$\alpha_2$
$\beta$
$\gamma$
$\delta$
\end{filecontents}

\begin{document}
% read the table
\pgfplotstableread[header=false]{plot_titles.txt}{\mytable}
\begin{tikzpicture}
  \begin{groupplot}[group style={group size=3 by 3},
    view={0}{90},
    try min ticks=2,
    max space between ticks=50,
    width=3.5cm,
    height=3.5cm]
    \pgfplotsforeachungrouped \i/\j in { % the following table is generated by a script
    1 / 1,
    1 / 2,
    1 / 3,
    2 / 1,
    2 / 2,
    2 / 3,
    3 / 1,
    3 / 2,
    3 / 3}{\pgfmathtruncatemacro\b{int(\i - 1)}
          \ifnum\i=\j
            \pgfplotstablegetelem{\b}{0}\of\mytable
            \edef\Name{\pgfplotsretval}
            \edef\temp{
            \noexpand\nextgroupplot[title={\Name}]\noexpand\addplot coordinates {(0,0) (1,1) (2,2)};} % Line plot
            \temp
            \typeout{\i=\j}
          \else
            \ifnum\j>\i
                \edef\temp{\noexpand\nextgroupplot[group/empty plot]} % No plot
                \temp
                \typeout{\j>\i}
            \else
                \edef\temp{\noexpand\nextgroupplot\noexpand\addplot coordinates {(0,0) (1,2) (2,1)};} % Contour plot
                \temp
                \typeout{\j<\i}
            \fi
          \fi
    }
  \end{groupplot}
\end{tikzpicture}
\end{document}

enter image description here

The irun counter was not needed. Where did you get that from? ;-)

  • Thanks again! Oddly enough, you can't seem to use \mathrm{} in the titles, as it throws an error. For the time being I'll just use \text{}, but may look into why in the future if it bugs me. – chasely Apr 16 '18 at 0:22
  • @chasely I confirm your observation, and was not able to cure this with a reasonable amount of \noexpands. Nor am I able to fix the lualatex issue. These might really important questions for real experts like Christian Feuersänger or Torbjørn T.. – user121799 Apr 16 '18 at 1:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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