在 pgfplots 中对齐图

在 pgfplots 中对齐图

有人能帮我吗?我尝试在 pgfplots 中水平对齐两个图!在序言中我有:

平均能量*


\documentclass[12pt]{article}
\usepackage{pgfplots,subcaption}
\pgfplotsset{ compat=1.7,
dacrip plot/.style={ % Set common styles that can be applied to the axis environment
    x=2cm, y=2cm,                   % Unit vector length
    axis lines=middle,              % Axis lines go through (0,0)
    xtick=\empty, ytick=\empty,     % No tick marks
    ymin=0, ymax=1.75,              % Y axis range
    enlargelimits=upper,            % Increase upper range a bit
    clip=false,                     % Allow labels to extend beyond the axis
    disabledatascaling,             % Allow to draw TikZ objects without having to use (axis cs:<x>,<y>)
    x label style={anchor=north},   % Alignment of axis labels
    y label style={anchor=east},
    yticklabel style={align=right}
}
}    
\begin{document}

\begin{figure}[!h]\centering
\begin{subfigure}[t]{0.48\textwidth} \centering
\begin{tikzpicture}[baseline]
\begin{axis}[dacrip plot, xlabel=$f$, ylabel=$U$]
       \addplot [blue, ultra thick] coordinates {(0,0) (1,1) (2,1) };
        \draw [dashed] (0,1) -- (1,1) node [pos=0, anchor=east] {$U_{max}$};
   \end{axis}
\end{tikzpicture}
\subcaption{Plot 1}\label{fig:ufMna}
\end{subfigure}
\begin{subfigure}[t]{0.48\textwidth} \centering

\begin{tikzpicture}[baseline]
\begin{axis}[dacrip plot, xlabel=$n$, ylabel=$M$ ]
       \addplot [blue, ultra thick] coordinates {(0,1) (1,1)} parabola [bend at end] (2,0.3);
   \end{axis};

\end{tikzpicture}
\subcaption{Plot 2}\label{fig:ufMnb}
\end{subfigure}
\caption{2 Plots}
\label{fig:ufMn}
\end{figure}
\end{document}

在此处输入图片描述

问题是图 2 的 x 轴太短。另一个问题是 x 轴对齐了,但两个图的标题没有对齐!


***** 根据 OP 的片段修改

答案1

由于缺少标题,我无法编译您的所有内容,但这应该与标题一致:

在您的 xlabel 中添加\vphantom{f]将添加缺少的垂直空间来对齐子标题:

\documentclass[12pt]{article}
\usepackage{pgfplots,subcaption}
\pgfplotsset{ compat=1.7,
dacrip plot/.style={ % Set common styles that can be applied to the axis environment
    x=2cm, y=2cm,                   % Unit vector length
    axis lines=middle,              % Axis lines go through (0,0)
    xtick=\empty, ytick=\empty,     % No tick marks
    ymin=0, ymax=1.75,              % Y axis range
    enlargelimits=upper,            % Increase upper range a bit
    clip=false,                     % Allow labels to extend beyond the axis
    disabledatascaling,             % Allow to draw TikZ objects without having to use (axis cs:<x>,<y>)
    x label style={anchor=north},   % Alignment of axis labels
    y label style={anchor=east},
    yticklabel style={align=right}
}
}    
\begin{document}

\begin{figure}[!h]\centering
\begin{subfigure}[t]{0.48\textwidth} \centering
\begin{tikzpicture}[baseline]
\begin{axis}[dacrip plot, xlabel=$f$, ylabel=$U$]
       \addplot [blue, ultra thick] coordinates {(0,0) (1,1) (2,1) };
        \draw [dashed] (0,1) -- (1,1) node [pos=0, anchor=east] {$U_{max}$};
   \end{axis}
\end{tikzpicture}
\subcaption{Plot 1}\label{fig:ufMna}
\end{subfigure}
\begin{subfigure}[t]{0.48\textwidth} \centering

\begin{tikzpicture}[baseline]
\begin{axis}[dacrip plot, xlabel={$n$\vphantom{f}}, ylabel=$M$ , xmax=2]
       \addplot [blue, ultra thick] coordinates {(0,1) (1,1)} parabola [bend at end] (2,0.3);
   \end{axis};

\end{tikzpicture}
\subcaption{Plot 2}\label{fig:ufMnb}
\end{subfigure}
\caption{2 Plots}
\label{fig:ufMn}
\end{figure}
\end{document}

我认为 TeX 会使用边界框来定位图片和标题,但在你的情况下,你的 2 张图片的大小(此处为深度)并不相同。\vphantom在 xlabel 中添加一个应该可以添加所需的空间来对齐每个图形的底部。

编辑:对于第二个问题,您可以通过键手动调整 x 轴xmax

输出

相关内容