将 pgflots 与轴对齐作为基线

将 pgflots 与轴对齐作为基线

考虑以下代码片段:

\documentclass{article}
\pagestyle{empty}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}[scale=0.35,baseline=(current axis.south)]
    \begin{axis}[axis lines=none,samples=100,domain=-10*pi:10*pi]
    \addplot [mark=none]{sin(deg(x-pi))/(x-pi)}; 
    \addplot [mark=none]{sin(deg(x+pi))/(x+pi)}; 
    \addplot [mark=none,color=red]{sin(deg(x+pi))/(x+pi) + sin(deg(x-pi))/(x-pi)}; 
    \draw[dashed,blue] (axis cs:pi,-5) -- (axis cs:pi,20);
    \draw[dashed,blue] (axis cs:-pi,-5) -- (axis cs:-pi,20);
    \end{axis}
\end{tikzpicture}
\begin{tikzpicture}[scale=0.35,baseline=(current axis.south)]
    \begin{axis}[axis lines=none,samples=100,domain=-10*pi:10*pi]
    \addplot [mark=none]{sin(deg(0.5*(x-pi)))/(0.5*(x-pi))}; 
    \addplot [mark=none]{sin(deg(0.5*(x+pi)))/(0.5*(x+pi))}; 
    \addplot [mark=none,color=red]{sin(deg(0.5*(x+pi)))/(0.5*(x+pi)) + sin(deg(0.5*(x-pi)))/(0.5*(x-pi))}; 
    \draw[dashed,blue] (axis cs:pi,-5) -- (axis cs:pi,20);
    \draw[dashed,blue] (axis cs:-pi,-5) -- (axis cs:-pi,20);
    \end{axis}
\end{tikzpicture}
test
\end{document}

这将产生以下输出(裁剪):

输出

我希望这些图片垂直对齐,使得 x 轴位于同一高度并形成以下文本(“测试”)的基线。

我的理解是baseline=(current axis.south)应该这样做,但显然我错了。

答案1

就像这样:

在此处输入图片描述

\documentclass{article}
\pagestyle{empty}
\usepackage{pgfplots}
\pgfplotsset{compat=1.12}
\begin{document}

\begin{tikzpicture}[scale=.9] % to suppress Overfull \hbox
    \begin{axis}[axis lines=none,samples=150,domain=-10*pi:10*pi,ymin=-.5, ymax=1.5]
    \addplot [mark=none]{sin(deg(x-pi))/(x-pi)}; 
    \addplot [mark=none]{sin(deg(x+pi))/(x+pi)}; 
    \addplot [mark=none,color=red]{sin(deg(x+pi))/(x+pi) + sin(deg(x-pi))/(x-pi)}; 
    \draw[dashed,blue] (axis cs:pi,-5) -- (axis cs:pi,20);
    \draw[dashed,blue] (axis cs:-pi,-5) -- (axis cs:-pi,20);
    \end{axis}
\end{tikzpicture}
\qquad
\begin{tikzpicture}[scale=.9]    
    \begin{axis}[axis lines=none,samples=150,domain=-10*pi:10*pi,ymin=-.5, ymax=1.5]
    \addplot [mark=none]{sin(deg(0.5*(x-pi)))/(0.5*(x-pi))}; 
    \addplot [mark=none]{sin(deg(0.5*(x+pi)))/(0.5*(x+pi))}; 
    \addplot [mark=none,color=red]{sin(deg(0.5*(x+pi)))/(0.5*(x+pi)) + sin(deg(0.5*(x-pi)))/(0.5*(x-pi))}; 
    \draw[dashed,blue] (axis cs:pi,-5) -- (axis cs:pi,20);
    \draw[dashed,blue] (axis cs:-pi,-5) -- (axis cs:-pi,20);
    \end{axis}
\end{tikzpicture}

\end{document}

可以current axis.south是对当前环境中的标签或节点的引用tikzpicture,但不能保留对未来的引用tikzpicture

这里,由于水平轴实际上位于 y = 0,因此对齐轴的最简单方法是设置相同的 y 范围,例如ymin=-.5, ymax=1.5。对于最终版本,还请考虑增加平滑度的样本数量(200+ 非常平滑)。另一个兼容性概念是设置\pgfplotsset{compat=newest}

相关内容