如何在两个 pgfpot 中对齐 x 轴和 y 轴?

如何在两个 pgfpot 中对齐 x 轴和 y 轴?

我有两个并排的 pgfplots,我想水平对齐 x 轴,如果我将两个图一个放在另一个上面,我想垂直对齐 y 轴。这是我的工作示例。如果将来图的长度(和宽度)会发生变化,则手动指定 x 和 y 移位会很麻烦。需要一种更优雅、更易理解的机制。

\documentclass{beamer}
\usetheme{Boadilla}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{shapes, arrows, positioning, calc}

\begin{document}
\begin{frame}{Example}
 The impulse response of a linear time-invariant system is $$h(n) = \{1, \underset{\uparrow}{2}, 1, -1 \}$$
 Determine the response of the system to the input signal $$x(n) = \{\underset{\uparrow}{1}, 2, 3, 1 \}$$
\begin{center}
\begin{tikzpicture}[scale=0.5]
\begin{axis}[axis x line=middle,
    x axis line style={thick},
    enlarge x limits,
    axis y line=middle,
    y axis line style={thick},ytick=\empty,
    tick style={draw=none},
    xlabel={$k$},
    ylabel={$x(k)$},
    nodes near coords={\pgfmathparseFPU{\pgfplotspointmeta}%
    \ifdim\pgfmathresult pt=0pt\else \pgfmathprintnumber\pgfplotspointmeta\fi},
    nodes near coords align=left,
    point meta = y,
    title={Plot of $x(k)$}
    ]
\addplot[ycomb, color=blue, mark=o, very thick] coordinates { (-2,0) (-1,0) (0,1) (1,2) (2,3) (3,1) (4,0) (5,0)};
\end{axis}
\end{tikzpicture}
\qquad
\begin{tikzpicture}[scale=0.5]
\begin{axis}[axis x line=middle,
    x axis line style={thick},
    enlarge x limits,
    axis y line=middle,
    y axis line style={thick},ytick=\empty,
    tick style={draw=none},
    xlabel={$k$},
    ylabel={$h(k)$},
    nodes near coords={\pgfmathparseFPU{\pgfplotspointmeta}%
    \ifdim\pgfmathresult pt=0pt\else \pgfmathprintnumber\pgfplotspointmeta\fi},
    nodes near coords align=left,
    point meta = y,
    title={Plot of $h(k)$}
    ]
\addplot[ycomb, color=green, mark=o, very thick] coordinates {(-3,0) (-2,0) (-1,1) (0,2) (1,1) (2,-1) (3,0) (4,0)};
\end{axis}
\end{tikzpicture}
\end{center}    
\end{frame}

\begin{frame}{Example}
\begin{center}
\begin{tikzpicture}[scale=0.5]
\begin{axis}[axis x line=middle,
    x axis line style={thick},
    enlarge x limits,
    axis y line=middle,
    y axis line style={thick},ytick=\empty,
    tick style={draw=none},
    xlabel={$k$},
    ylabel={$x(k)$},
    nodes near coords={\pgfmathparseFPU{\pgfplotspointmeta}%
    \ifdim\pgfmathresult pt=0pt\else \pgfmathprintnumber\pgfplotspointmeta\fi},
    nodes near coords align=left,
    point meta = y,
    title={Plot of $x(k)$},
    name = first
    ]
    \addplot[ycomb, color=blue, mark=o, very thick] coordinates { (-2,0) (-1,0) (0,1) (1,2) (2,3) (3,1) (4,0) (5,0)};
\end{axis}

\begin{axis}[axis x line=middle,
    x axis line style={thick},
    enlarge x limits,
    axis y line=middle,
    y axis line style={thick},ytick=\empty,
    tick style={draw=none},
    xlabel={$k$},
    ylabel={$h(k)$},
    nodes near coords={\pgfmathparseFPU{\pgfplotspointmeta}%
    \ifdim\pgfmathresult pt=0pt\else \pgfmathprintnumber\pgfplotspointmeta\fi},
    nodes near coords align=left,
    point meta = y,
    title={Plot of $h(k)$}
    at=(first.below south west), 
    anchor=north west,
    yshift = -1.5cm,
    name = second
    ]
\addplot[ycomb, color=green, mark=o, very thick] coordinates {(-3,0) (-2,-1) (-1,1) (0,2) (1,1) (2,0) (3,0) (4,0)};
\end{axis}
\end{tikzpicture}
\end{center}    
\end{frame}
\end{document}

答案1

规范的方法是使用groupplots

\documentclass{beamer}
\usetheme{Boadilla}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\usepgfplotslibrary{groupplots}
\newcommand{\pgfmathparseFPU}[1]{\begingroup%
 \pgfkeys{/pgf/fpu,/pgf/fpu/output format=fixed}%
 \pgfmathparse{#1}%
 \pgfmathsmuggle\pgfmathresult\endgroup}
\makeatletter
\newcommand{\ReportVShift}[2][]{%
 \immediate\write\@auxout{\xdef\string\myshift#1{#2}}%
}
\makeatother 
\usepackage{amsmath}
\usetikzlibrary{calc}

\begin{document}
\begin{frame}{Example}
 The impulse response of a linear time-invariant system is \[h(n) = \{1,
 \underset{\uparrow}{2}, 1, -1 \}\]
 Determine the response of the system to the input signal \[x(n) =
 \{\underset{\uparrow}{1}, 2, 3, 1 \}\]
\begin{center}
\begin{tikzpicture}
\ifcsname myshiftA\endcsname
\edef\myshift{\myshiftA}
\else
\edef\myshift{0pt}
\fi
\begin{groupplot}[group style={group size=2 by 1,horizontal sep=6mm},
    height=3.5cm,width=5.5cm,
    axis x line=middle,,
    x axis line style={thick},
    enlarge x limits,
    axis y line=middle,
    y axis line style={thick},ytick=\empty,
    tick style={draw=none},
    xlabel={$k$},    
    nodes near coords={\pgfmathparseFPU{\pgfplotspointmeta}%
    \ifdim\pgfmathresult pt=0pt\else \pgfmathprintnumber\pgfplotspointmeta\fi},
    nodes near coords align=left,
    point meta = y]
\nextgroupplot[title={Plot of $x(k)$},ylabel={$x(k)$}]
\addplot[ycomb, color=blue, mark=o, very thick] coordinates { (-2,0) (-1,0) (0,1) (1,2) (2,3) (3,1) (4,0) (5,0)};
\path (0,0) coordinate (OL);
%
\nextgroupplot[title={Plot of $h(k)$},ylabel={$h(k)$},yshift=\myshift]
\addplot[ycomb, color=green, mark=o, very thick] coordinates {(-3,0) (-2,0) (-1,1) (0,2) (1,1) (2,-1) (3,0) (4,0)};
\path (0,0) coordinate (OR) let \p1=($(OL)-(OR)$)
  in \pgfextra{\ReportVShift[A]{\y1}};
\end{groupplot}
\end{tikzpicture}
\end{center}    
\end{frame}

\begin{frame}{Example}
\centering
\begin{tikzpicture}
\begin{groupplot}[group style={group size=1 by 2,vertical sep=6mm},
    height=3cm,height=3.5cm,width=7cm,
    axis x line=middle,
    x axis line style={thick},
    enlarge x limits,
    axis y line=middle,
    y axis line style={thick},ytick=\empty,
    tick style={draw=none},
    xlabel={$k$},    
    nodes near coords={\pgfmathparseFPU{\pgfplotspointmeta}%
    \ifdim\pgfmathresult pt=0pt\else \pgfmathprintnumber\pgfplotspointmeta\fi},
    nodes near coords align=left,
    point meta = y]
\nextgroupplot[title={Plot of $x(k)$},ylabel={$x(k)$}]
    \addplot[ycomb, color=blue, mark=o, very thick] coordinates { (-2,0) (-1,0) (0,1) (1,2) (2,3) (3,1) (4,0) (5,0)};
%
\nextgroupplot[title={Plot of $h(k)$},ylabel={$h(k)$}]
    \addplot[ycomb, color=green, mark=o, very thick] coordinates {(-3,0) (-2,-1) (-1,1) (0,2) (1,1) (2,0) (3,0) (4,0)};
\end{groupplot} 
\end{tikzpicture}
\end{frame}
\end{document}

但是,为了实现对齐,我必须报告两个轴原点的 y 坐标差异。因此,这里有两个替代方案,您不必这样做:设置适当的基线并使用matrix

\documentclass{beamer}
\usetheme{Boadilla}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\newcommand{\pgfmathparseFPU}[1]{\begingroup%
 \pgfkeys{/pgf/fpu,/pgf/fpu/output format=fixed}%
 \pgfmathparse{#1}%
 \pgfmathsmuggle\pgfmathresult\endgroup}
\usepackage{amsmath}
% \usepackage{tikz}
% \usetikzlibrary{shapes, arrows, positioning, calc}

\begin{document}
\begin{frame}{Example}
 The impulse response of a linear time-invariant system is \[h(n) = \{1,
 \underset{\uparrow}{2}, 1, -1 \}\]
 Determine the response of the system to the input signal \[x(n) =
 \{\underset{\uparrow}{1}, 2, 3, 1 \}\]
\begin{center}
\begin{tikzpicture}[baseline={(OL)}]
\begin{axis}[axis x line=middle,height=3.5cm,width=5.5cm,
    x axis line style={thick},
    enlarge x limits,
    axis y line=middle,
    y axis line style={thick},ytick=\empty,
    tick style={draw=none},
    xlabel={$k$},
    ylabel={$x(k)$},
    nodes near coords={\pgfmathparseFPU{\pgfplotspointmeta}%
    \ifdim\pgfmathresult pt=0pt\else \pgfmathprintnumber\pgfplotspointmeta\fi},
    nodes near coords align=left,
    point meta = y,
    title={Plot of $x(k)$}
    ]
\addplot[ycomb, color=blue, mark=o, very thick] coordinates { (-2,0) (-1,0) (0,1) (1,2) (2,3) (3,1) (4,0) (5,0)};
\path (0,0) coordinate (OL);
\end{axis}
\end{tikzpicture}
\qquad
\begin{tikzpicture}[baseline={(OR)}]
\begin{axis}[axis x line=middle,height=3.5cm,width=5.5cm,
    x axis line style={thick},
    enlarge x limits,
    axis y line=middle,
    y axis line style={thick},ytick=\empty,
    tick style={draw=none},
    xlabel={$k$},
    ylabel={$h(k)$},
    nodes near coords={\pgfmathparseFPU{\pgfplotspointmeta}%
    \ifdim\pgfmathresult pt=0pt\else \pgfmathprintnumber\pgfplotspointmeta\fi},
    nodes near coords align=left,
    point meta = y,
    title={Plot of $h(k)$}
    ]
\addplot[ycomb, color=green, mark=o, very thick] coordinates {(-3,0) (-2,0) (-1,1) (0,2) (1,1) (2,-1) (3,0) (4,0)};
\path (0,0) coordinate (OR);
\end{axis}
\end{tikzpicture}
\end{center}    
\end{frame}

\begin{frame}{Example}
\centering
\begin{tikzpicture}
\matrix{
\begin{axis}[axis x line=middle,height=3.5cm,width=7cm,
    x axis line style={thick},
    enlarge x limits,
    axis y line=middle,
    y axis line style={thick},ytick=\empty,
    tick style={draw=none},
    xlabel={$k$},
    ylabel={$x(k)$},
    nodes near coords={\pgfmathparseFPU{\pgfplotspointmeta}%
    \ifdim\pgfmathresult pt=0pt\else \pgfmathprintnumber\pgfplotspointmeta\fi},
    nodes near coords align=left,
    point meta = y,
    title={Plot of $x(k)$},
    name = first
    ]
    \addplot[ycomb, color=blue, mark=o, very thick] coordinates { (-2,0) (-1,0) (0,1) (1,2) (2,3) (3,1) (4,0) (5,0)};
\end{axis}\\

\begin{axis}[axis x line=middle,height=3.5cm,width=7cm,
    x axis line style={thick},
    enlarge x limits,
    axis y line=middle,
    y axis line style={thick},ytick=\empty,
    tick style={draw=none},
    xlabel={$k$},
    ylabel={$h(k)$},
    nodes near coords={\pgfmathparseFPU{\pgfplotspointmeta}%
    \ifdim\pgfmathresult pt=0pt\else \pgfmathprintnumber\pgfplotspointmeta\fi},
    nodes near coords align=left,
    point meta = y,
    title={Plot of $h(k)$}
    at=(first.below south west), 
    anchor=north west,
    yshift = -1.5cm,
    name = second
    ]
\addplot[ycomb, color=green, mark=o, very thick] coordinates {(-3,0) (-2,-1) (-1,1) (0,2) (1,1) (2,0) (3,0) (4,0)};
\path (0,0) coordinate (OR);
\end{axis} \\
};
\end{tikzpicture}
\end{frame}
\end{document}

在此处输入图片描述

顺便说一句,不要使用scale密钥,而是适当设置heightwidth。这种方法肯定不比更好groupplots

相关内容