0

I have tried to align two tikz-subfigures. For for some reason though it doesn't work completely and they are misaligned horizontally as per this screenshot.

What am I doing wrong here?

\documentclass[12pt, a4paper]{article}
\usepackage[english]{babel}

\usepackage{amsmath} \usepackage{amsfonts} \usepackage{graphicx} \usepackage{multirow} \usepackage{booktabs} \usepackage{tabularx} \usepackage{array} \usepackage{subcaption} \usepackage[hmargin=1in, vmargin=1.2in]{geometry} \usepackage[labelfont=bf]{caption} \captionsetup{width=.98\linewidth}

\usepackage{setspace} \setstretch{1.3}

\usepackage{pgfplots} \usepackage{tikz} \pgfplotsset{compat=1.18}

\begin{document}

\begin{figure}[h] \begin{subfigure}{\linewidth} \centering \begin{tikzpicture} \begin{axis}[ scaled y ticks = false, y tick label style={/pgf/number format/fixed, /pgf/number format/fixed zerofill, /pgf/number format/precision=2}, ]

    \addplot[color=black] coordinates {
        (1, 1)
        (2, 2)
        (3, 3)
        (4, 4)
        (5, 5)
        (6, 6)
        (7, 7)
        (8, 8)
      };
  \end{axis}

\end{tikzpicture}
\caption{}

\end{subfigure}% \[5mm] \begin{subfigure}{\linewidth}% \centering \begin{tikzpicture} \begin{axis}[ scaled y ticks = false, y tick label style={/pgf/number format/fixed, /pgf/number format/fixed zerofill, /pgf/number format/precision=2}, ]

    \addplot[color=black] coordinates {
        (-100, -1)
        (200, 2)
        (300, 3)
      };
  \end{axis}

\end{tikzpicture}
\caption{}

\end{subfigure} % \caption{asd} \end{figure}

\end{document}

enter image description here

TylerD
  • 123
  • 4
  • 1
    Replace \\ \vspace{5mm} by \\[5mm]. – dexteritas Oct 18 '23 at 13:30
  • @dexteritas I made the change and it worked. But I then made some changes to the input-data, and then it stopped working (see the updated OP) - can you spot why? – TylerD Oct 18 '23 at 13:39
  • See my first comment here https://tex.stackexchange.com/questions/691966/align-tikzpicture-plots-subfigures-horizontally-and-vertically about the causes, and a fine solution by another member of this community. – MS-SPO Oct 18 '23 at 20:44

2 Answers2

1

You must expand the width assigned to the y-labels (in both plots) to accommodate the minus sign.

b

\documentclass[12pt, a4paper]{article}
\usepackage[english]{babel}

\usepackage{amsmath} \usepackage{amsfonts} \usepackage{graphicx} \usepackage{multirow} \usepackage{booktabs} \usepackage{tabularx} \usepackage{array} \usepackage{subcaption} \usepackage[hmargin=1in, vmargin=1.2in]{geometry} \usepackage[labelfont=bf]{caption} \captionsetup{width=.98\linewidth}

\usepackage{setspace} \setstretch{1.3}

\usepackage{pgfplots} \usepackage{tikz} \pgfplotsset{compat=1.18}

\begin{document}

\begin{figure}[h]
    \pgfplotsset{yticklabel style = {text width=2.5em, align=right}}% added <<<<<<<<<<<<        

    \begin{subfigure}{\linewidth}
    \centering
    \begin{tikzpicture}
        \begin{axis}[
            scaled y ticks = false,
            y tick label style={/pgf/number format/fixed, /pgf/number format/fixed zerofill, /pgf/number format/precision=2},
            ]

            \addplot[color=black] coordinates {
                (1, 1)
                (2, 2)
                (3, 3)
                (4, 4)
                (5, 5)
                (6, 6)
                (7, 7)
                (8, 8)
            };
        \end{axis}

    \end{tikzpicture}
    \caption{}
\end{subfigure}%
\\[5mm]
\begin{subfigure}{\linewidth}%
    \centering
    \begin{tikzpicture}
        \begin{axis}[
            scaled y ticks = false,
            y tick label style={/pgf/number format/fixed, /pgf/number format/fixed zerofill, /pgf/number format/precision=2},
            ]

            \addplot[color=black] coordinates {
                (-100, -1)
                (200, 2)
                (300, 3)
            };
        \end{axis}

    \end{tikzpicture}
    \caption{}
\end{subfigure}  %
\caption{asd}

\end{figure}

\end{document}

Simon Dispa
  • 39,141
0

This is happening because you are using \centering and your plots are not precisely the same width: the minus sign in the bottom figure makes it just slightly wider. It is almost but not quite perfectly lined up if I put \phantom{+} right before the first tikzpicture environment, and you could replace that or augment it with \hspace commands to get it to line up perfectly by hand.

karlh
  • 640