我如何通过 y 轴对齐这两个图形?

我如何通过 y 轴对齐这两个图形?

如何垂直对齐这两个图表?它们的 y 轴不是在此处输入图片描述对齐,这让我很烦。另外,我尝试过“[修剪轴左,修剪轴右,基线]”,正如我在这里读到的,但它不起作用:

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{subcaption}


\begin{document}

\begin{figure}[ht]
    \begin{subfigure}{.5\textwidth}
        \centering
        \begin{tikzpicture}
            \begin{axis}[trim axis left,trim axis right,baseline,
            height=6cm, width=.95\textwidth, axis lines=middle,
            ytick={0},
            xlabel={$x$}, xmax=1.2, axis on top
            ]
                \addplot[samples=100, domain=0:1,]{sin(deg(2*x*pi))};
                \path[fill=gray!10] (1,-1)--(1,1)--(1.2,1)--(1.2,-1);
                \draw (1,-1) -- (1,1);
            \end{axis}      
        \end{tikzpicture}
        \caption{}
    \end{subfigure}
    \begin{subfigure}{.5\textwidth}
        \centering
        \begin{tikzpicture}
            \begin{axis}[trim axis left,trim axis right,baseline,
            height=6cm, width=.95\textwidth, axis lines=middle,
            ytick={0},
            xlabel={$x$}, xmax=1.2, axis on top
            ]
                \addplot[samples=100, domain=0:1]{(sin(deg(2*x*pi)))^2};
                \path[fill=gray!10] (1,-1)--(1,1)--(1.2,1)--(1.2,-1);
                \draw (1,-1) -- (1,1);
            \end{axis}      
        \end{tikzpicture}
        \caption{}
    \end{subfigure}
    \caption{}
\end{figure}

\end{document}

答案1

您需要将图像基线移动到其垂直中心。这预期两个图像的高度相同。然而,这似乎不是这种情况,因此请尝试将基线移动到图像顶部,并将其与subfigure图像顶部对齐:

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{subcaption}


\begin{document}

\begin{figure}[ht]
    \begin{subfigure}[t]{.5\textwidth}
        \centering
        \begin{tikzpicture}[baseline=(current bounding box.north)]
            \begin{axis}[trim axis left,trim axis right,baseline,
            height=6cm, width=.95\textwidth, axis lines=middle,
            ytick={0},
            xlabel={$x$}, xmax=1.2, axis on top
            ]
                \addplot[samples=100, domain=0:1,]{sin(deg(2*x*pi))};
                \path[fill=gray!10] (1,-1)--(1,1)--(1.2,1)--(1.2,-1);
                \draw (1,-1) -- (1,1);
            \end{axis}
        \end{tikzpicture}
        \caption{}
    \end{subfigure}%
    \begin{subfigure}[t]{.5\textwidth}
        \centering
        \begin{tikzpicture}[baseline=(current bounding box.north)]
            \begin{axis}[trim axis left,trim axis right,baseline,
            height=6cm, width=.95\textwidth, axis lines=middle,
            ytick={0},
            xlabel={$x$}, xmax=1.2, axis on top
            ]
                \addplot[samples=100, domain=0:1]{(sin(deg(2*x*pi)))^2};
                \path[fill=gray!10] (1,-1)--(1,1)--(1.2,1)--(1.2,-1);
                \draw (1,-1) -- (1,1);
            \end{axis}
        \end{tikzpicture}
        \caption{}
    \end{subfigure}
    \caption{}
\end{figure}

\end{document}

在此处输入图片描述

答案2

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{subcaption}


\begin{document}

\begin{figure}[ht]
    \begin{subfigure}{.5\textwidth}
        \centering
        \begin{tikzpicture}
            \begin{axis}[trim axis left,trim axis right,baseline,
            height=6cm, width=.95\textwidth, axis lines=middle,
            ytick={0},
            xlabel={$x$}, xmax=1.2, axis on top
            ]
                \addplot[samples=100, domain=0:1,]{sin(deg(2*x*pi))};
                \path[fill=gray!10] (1,-1)--(1,1)--(1.2,1)--(1.2,-1);
                \draw (1,-1) -- (1,1);
            \end{axis}      
          \end{tikzpicture}
          \vspace{6.2mm}
        \caption{}
    \end{subfigure}
    \begin{subfigure}{.5\textwidth}
        \centering
        \begin{tikzpicture}
            \begin{axis}[trim axis left,trim axis right,baseline,
            height=6cm, width=.95\textwidth, axis lines=middle,
            ytick={0},
            xlabel={$x$}, xmax=1.2, axis on top
            ]
                \addplot[samples=100, domain=0:1]{(sin(deg(2*x*pi)))^2};
                \path[fill=gray!10] (1,-1)--(1,1)--(1.2,1)--(1.2,-1);
                \draw (1,-1) -- (1,1);
            \end{axis}      
        \end{tikzpicture}
        \caption{}
    \end{subfigure}
    \caption{}
\end{figure}

\end{document}

相关内容