Conditional plotting by position in pgfplots groupplot

Edit: I was able to get the layout in the last image following the accepted answer. Just needs some visual tweaking now. I have some issues with compilation with LuaLatex, which I will troubleshoot but update this if I open another question.

I am trying to plot a matrix of probability densities (shown below) using pgfplots. The resulting figure is a lower-triangular matrix of plots, with normal line plots on the diagonal and filled contour plots below the diagonal. I figured the best way to do this would be to use groupplot.

I have data in files for each of the plots, and can plot them individually but can't figure out how to get them in the right position in the groupplot.

If i is columns and j is rows, then when i>j there should be no plot, when i=j there should be the line plot, and when i<j there is the countour plot.

I tried to write something that would match these conditions, then I can just add the correct call to the plots, but right now this code will not compile even with blank plots.

Based on values of i and j, I can call the files I need within a plot command, (e.g., \addplot3[contour filled={number=14},mesh/rows=50] table {kde_2d_\i_\b.dat}, where b is a modified value of j to get the plot in the right direction. I know that these plots work because I can plot them individually or in a group plot without any ordering, but the image won't compile once I include conditional statements.

Any tipss on where I should be looking to fix the issue?

\documentclass{standalone}
\usepackage{pgfplots,pgfplotstable}
\usepackage{etoolbox}
\usepgfplotslibrary{groupplots} % LATEX and plain TEX

\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]
\def\myPlots{}
\pgfplotsforeachungrouped \i in {1,...,3}
{
\pgfplotsforeachungrouped \j in {1,...,\i}
{
\pgfmathsetmacro\b{int(\j - 1)}
\eappto\myPlots{%
\ifnum\i=\j
\noexpand\nextgroupplot[group/empty plot] % Line plot
\else
\ifnum\i>\j
\noexpand\nextgroupplot[group/empty plot] % No plot
\else
\noexpand\nextgroupplot[group/empty plot] % Contour plot
\fi
\fi
}
}
}
\myPlots
\end{groupplot}
\end{tikzpicture}
\end{document}


• Why does \j run only up to \i and you add empty plots for \i>\j? Shouldn't \j run all the way to 3 instead? – user121799 Apr 14 '18 at 23:38

There are various expansion issues. I ended up to use one loop instead of two to make it work.

\documentclass[border=3.14pt]{standalone}
\usepackage{pgfplots,pgfplotstable}
\usepgfplotslibrary{groupplots}
\pgfplotsset{compat=1.15}
\newcounter{irun}
\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}


I put some small plots just to show that this method discriminates between the diagonal, below and above.

• This hasn't worked for me yet, I suspect it might be because I'm on pgfplots 1.14. Need to upgrade my MacTeX and test with the newer version of pgfplots. – chasely Apr 15 '18 at 3:20
• @chasely Sorry to hear that. What error message do you get? And yes, upgrading pgfplots might be a good idea. – user121799 Apr 15 '18 at 3:32
• Looks like it was compiling with LuaLatex. I can get it to compile (with errors) with pdflatex, so I'll count this as being solved, but open another question if I can't figure out the compiling issues. – chasely Apr 15 '18 at 4:45
• @chasely Strange. I compiled it with pdflatex. – user121799 Apr 15 '18 at 12:52
• Wow, sorry, it was late and that comment was completely wrong. I was having problems compiling with LuaLatex, which I was using because I was having the running out of memeory issues compiling with pdflatex due to the size of the data being plotted. – chasely Apr 15 '18 at 13:28