0

I'm trying to produce a 2 colums by 2/3 rows groupplot in pgfplots. On the first column I have 2 plots, on the second I have 3 plots. I want to distribute the plot heights for the side with fewer plots to have the same outer bounding box.

Uneven number groupplot with vertial alignment problem.

Using two different heights for the first and second column plots I can, at least somewhat, make them fit in total height. However, vertically the first row is aligned on the centers of the two first-row plots (a & b). What I want to achieve is, that the bounding box top of the first row is aligned vertically and the bounding box bottom line is aligned for the last plot.

I found anchor=north here, but it changes only the right plot's position (b), that is then aligned with the left vertical center (of plot a). Even if I add the option for both plots.

My code looks somewhat like:

\documentclass{standalone}
% pictures drawn with TikZ
\usepackage{tikz,pgfplots}
\usetikzlibrary{pgfplots.groupplots} % needs to be loaded exactly like this
\pgfplotsset{compat=newest}

\begin{document} \newcommand{\heightLarge}{4.7cm}

\begin{tikzpicture} \begin{groupplot} [ group style={group size=2 by 3, % cols by rows horizontal sep=50pt, }, width= 0.385\textwidth, height=3.2cm, xmin=0, xmax=14, xlabel={time in s}, ]

\nextgroupplot[% #1 ymin=0.0, ymax=2/2, ylabel={a}, height=\heightLarge, ] \addplot[]{sin(x)};

\nextgroupplot[% #2 ymin=0, ymax=0.2/0.03, ylabel={b}, ] \addplot[]{sin(x)};

\nextgroupplot[% #3 ymin=-0.8, ymax=0.8, height=\heightLarge, ] \addplot[]{sin(x)};

\nextgroupplot[% #4 ymin=2.5/3, ymax=4.2/3, ] \addplot[]{sin(x)};

\nextgroupplot[group/empty plot] % #5

\nextgroupplot[% #6 ymin=0, ymax=60, ] \addplot[]{sin(x)};

\end{groupplot} \end{tikzpicture}% \end{document}

mike
  • 333
  • 1
  • 3
  • 13
  • Welcome to TeX.SX! Please make your code compilable (if possible), or at least complete it with \documentclass{...}, the required \usepackage's, \begin{document}, and \end{document}. That may seem tedious to you, but think of the extra work it represents for TeX.SX users willing to give you a hand. Help them help you: remove that one hurdle between you and a solution to your problem. – hpekristiansen Jan 04 '23 at 16:44
  • Yes, you are right, thanks. – mike Jan 04 '23 at 16:50

1 Answers1

2

I do not think that groupplot can be used to make a layout like this. Luckily it is not too difficult to do manually.

\documentclass[tikz, border=1cm]{standalone}
\usetikzlibrary{calc}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\pgfplotsset{
xmin=0, xmax=14, 
xlabel={time in s}, xlabel style={yshift=4pt},
}
\begin{axis}[
name=plot1,
width=5cm, height=5cm,
ymin=0, ymax=1,
]
\end{axis}
\begin{axis}[
name=plot2, at={($(plot1.south west)+(0,-1cm)$)}, anchor=north west,
width=5cm, height=5cm,
ymin=-1, ymax=1,
]
\end{axis}
\begin{axis}[
name=plot3, at={($(plot1.north east)+(1.5cm,0)$)}, anchor=north west,
width=5cm, height=3.5cm,
ymin=0, ymax=6,
]
\end{axis}
\begin{axis}[
name=plot4, at={($(plot1.north east)!0.5!(plot2.south east)+(1.5cm,0)$)}, anchor=west,
width=5cm, height=3.5cm,
ymin=0.5, ymax=1.4,
]
\end{axis}
\begin{axis}[
name=plot5, at={($(plot2.south east)+(1.5cm,0)$)}, anchor=south west,
width=5cm, height=3.5cm,
ymin=0, ymax=60,
]
\end{axis}
\end{tikzpicture}
\end{document}

Five empty graphs in a grid

Edit: A layout with seven graphs

\documentclass[tikz, border=1cm]{standalone}
\usetikzlibrary{calc}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\pgfplotsset{xmin=0, xmax=1, ymin=0, ymax=1, width=5cm, height=3cm}
\begin{axis}[name=plot1] \end{axis}
\begin{axis}[name=plot2, at={($(plot1.south west)+(0,-2cm)$)}, anchor=north west] \end{axis}
\begin{axis}[name=plot3, at={($(plot2.south west)+(0,-2cm)$)}, anchor=north west] \end{axis}
\begin{axis}[name=plot4, at={($(plot1.north east)+(1cm,0)$)},  anchor=north west] \end{axis}
\begin{axis}[name=plot7, at={($(plot3.south east)+(1cm,0)$)},  anchor=south west] \end{axis}
\begin{axis}[name=plot5, at={($(plot4)!1/3!(plot7)$)},  anchor=center] \end{axis}
\begin{axis}[name=plot6, at={($(plot4)!2/3!(plot7)$)},  anchor=center] \end{axis}
\end{tikzpicture}
\end{document}

Seven graphs in a grid

  • Wow, yeah, thanks! This is exactly what I needed. Coud you give me a hint how to realize the same thing for 3|4 rows? Since the middle two are the height-separation value apart I tried to put them at={($(plot1.north east)!0.5!(plot_last_left.south east)+(\horizontalSeparation,\verticalSeparation/2)$)},, but using the height 5cm, verticalSeparation 1 and height right 3.5 the plots are not distributed equally spaced. – mike Jan 05 '23 at 13:03
  • Preferably it would be calculated from the height of the left side plots and the vertical separation (equal for both sides). I tried \pgfmathparse{(3*\heightLeft-\verticalSeparation)/4} \let\heightRight\pgfmathresult, but it again produces results that are not of equal height. Also I cannot seem to think of another quantity that I could anchor the at of the middle two rows on the right. – mike Jan 05 '23 at 13:20
  • Count the number of graphs and the number of spaces in an interval. -there is not always the same number, so carefull considerations are needed. You are not forced to use the ($(plota)!1/3!(plotb)$) notation - you can calculate by hand the position of the plot. – hpekristiansen Jan 05 '23 at 13:39
  • Thanks for the answer, it works now. I used plots below each other on the right and centered the left middle position. What I don't understand though, is why the calculations and therefore the distances don't add up: I use 3.5cm height for the right plots and 1cm vertical separation, which makes 17cm total height. If I use 5cm height for the left plots their vertical separation is not 1cm, even though this would also add up to 17cm. – mike Jan 05 '23 at 15:31
  • I do not want to try to figure out, what you are doing. Remember width and height are for final dimensions by default. To set dimensions of the axis box, set option scale only axis=true for all axis. -or ask a new question, so I can see your code. – hpekristiansen Jan 05 '23 at 15:49
  • 1
    Take measurement with e.g. \draw[red, ultra thick] (plot7.south east) -- +(0,3.5cm); – hpekristiansen Jan 05 '23 at 15:55
  • Thank you very much. The measurements showed strange axis heights/widths and I found that the scale only axis=true setting was not properly set in my document wide figure settings file. Thanks again! – mike Jan 09 '23 at 13:02