Create a matrix of subfigures

I am trying to create a figure made of 7 subfigures, whose layout is:

3 subfigures on the first and second row (left aligned, centered and right aligned)

and one subfigure on the third row (left aligned).

The code I wrote is:

\begin{figure}[h]
\begin{subfigure}[t!]{0.4 \textwidth}
\includegraphics[scale=0.25]{./capitoli/4/immagini/a_1.png}

\end{subfigure}
\begin{subfigure}[t!]{0.4 \textwidth}
\includegraphics[scale=0.23]{./capitoli/4/immagini/a_2.png}
\end{subfigure}

\begin{subfigure}[t!]{0.2 \textwidth}
\includegraphics[scale=0.24]{./capitoli/4/immagini/a_3.png}

\end{subfigure}

\begin{subfigure}[c!]{0.25 \textwidth}
\includegraphics[scale=0.35]{./capitoli/4/immagini/a_4.png}
\end{subfigure}

\begin{subfigure}[c!]{0.2 \textwidth}
\includegraphics[scale=0.35]{./capitoli/4/immagini/a_5.png}
\end{subfigure}

\begin{subfigure}[c!]{0.2 \textwidth}
\includegraphics[scale=0.35]{./capitoli/4/immagini/a_6.png}
\end{subfigure}

\begin{subfigure}[b!]{0.2 \textwidth}
\includegraphics[scale=0.35]{./capitoli/4/immagini/a_7.png}
\end{subfigure}

\end{figure}


But it prints two subfigures on the first row and then one left-aligned subfigure for five rows.

Any tips to solve this issue?

• Try with subfloatrows environments, from package floatrow. – Bernard Jun 19 at 23:12
• Just eliminate the blank lines and throw in some \hfills. – John Kormylo Jun 20 at 3:34

Since you have different sized figures in addition to \subfigures of different widths, is difficult IMO to achieve your desired layout just adding for example \hfill and \\ (this works well for figures with the same size):

Code:

\documentclass{article}
\usepackage{graphicx}
\usepackage{subcaption}

\begin{document}
\begin{figure}[h]
\begin{subfigure}[t]{0.4\textwidth}
\includegraphics[scale=0.25]{example-image-a}
\end{subfigure}%
\hfill
\begin{subfigure}[t]{0.4\textwidth}
\centering
\includegraphics[scale=0.23]{example-image-a}
\end{subfigure}%
\hfill
\begin{subfigure}[t]{0.2\textwidth}
\includegraphics[scale=0.24]{example-image-a}
\end{subfigure}%
\\
\begin{subfigure}[b]{0.25\textwidth}
\includegraphics[scale=0.35]{example-image-b}
\end{subfigure}%
\hfill
\begin{subfigure}[b]{0.2\textwidth}
\includegraphics[scale=0.35]{example-image-b}
\end{subfigure}%
\hfill
\begin{subfigure}[b]{0.2\textwidth}
\includegraphics[scale=0.35]{example-image-b}
\end{subfigure}%
\\
\begin{subfigure}[b]{0.3\textwidth}
\includegraphics[scale=0.35]{example-image-c}
\end{subfigure}%
\hfill
\end{figure}
\end{document}


Therefore, a simple approach according to the output of your code is using the tabular environment. With l, c and r you can align the figures on the left, center or right; respectively.

Output:

Code:

\documentclass{article}
\usepackage{graphicx}
\usepackage{subcaption}

\begin{document}
\begin{figure}[h]
\centering
\begin{tabular}{lcr}
\includegraphics[scale=0.25]{example-image-a} &
\includegraphics[scale=0.23]{example-image-a} &
\includegraphics[scale=0.24]{example-image-a} \\
\includegraphics[scale=0.35]{example-image-b} &
\includegraphics[scale=0.35]{example-image-b} &
\includegraphics[scale=0.35]{example-image-b} \\
\includegraphics[scale=0.35]{example-image-c} & & \\
\end{tabular}
\end{figure}
\end{document}


From presented images in your question I assume that:

• they have equal size,
• they haven't sub captions

so it can be simply arranged in figure as follows:

\documentclass{article}
\usepackage{graphicx}

\begin{document}
\begin{figure}[htb]
\setkeys{Gin}{width=0.3\linewidth}
\includegraphics{example-image-duck}
\hfill
\includegraphics{example-image-duck}
\hfill
\includegraphics{example-image-duck}

\smallskip
\includegraphics{example-image-duck}
\hfill
\includegraphics{example-image-duck}
\hfill
\includegraphics{example-image-duck}

\smallskip
\includegraphics{example-image-duck}

\caption{My beautiful images. Quack!}
\label{tab:traintestsplit}
\end{figure}
\end{document}


Note, empty line between images or text start new text line.