PGFPlots:多个图形对齐中的问题

PGFPlots:多个图形对齐中的问题

我的问题与前一个有关:使用 pgfplots 和 subfig 包进行图形对齐的问题。我采用了推荐的命令heightymax/ymin 来调整三个图表的 y 轴比例(并使其大小标准化)。但显然大小相等并不意味着调整正确。这非常微妙,但似乎从下往下,y 轴正在向左移动(我拿了一把尺子来比较)。我想问你我遗漏了什么。代码包含巴西学术作品规范的模板,仅供参考。

正弦图

    \documentclass[
            % -- opções da classe memoir --
            12pt,
            openright,
            oneside,
            a4paper,
            % -- opções da classe abntex2 --
            chapter=TITLE,         
            section=TITLE,         
            subsection=Title,      
            % -- opções do pacote babel --
            english,                        
            french,                         
            spanish,                        
            portugues,                    
            %%%%%%%%%%%%
            sumario=tradicional             % título no formato tradicional
            ]{abntex2}
    \usepackage[T1]{fontenc}
    \usepackage[utf8]{inputenc}
    \usepackage{subfig}
    \usetikzlibrary{arrows, calc,patterns,angles,quotes}
    \usepackage{pgfplots}
    \pgfplotsset{compat=1.9}    
        \begin{tikzpicture}
            \begin{axis}[
                    width=8cm,height=4cm,
                    ymax = 12, ymin = -12,
                    xmax = 5,
                    axis x line=middle,
                    axis y line=middle, 
                    axis line style={-stealth},
                    xlabel = {$t$},xlabel style={at={(1,0.5)}, anchor=west},ylabel style={at={(0,1)}, anchor=south},
                    ylabel = {$x$},
                    %xtick distance = 90,
                    ytick = \empty,
                    xtick={0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4},
                    xticklabels={ , , , ,$\tau$, , , , $2\tau$},
            ]
            \addplot[domain=0:4.5, 
                    samples=100, 
                    color=black,
                    thick,]
                {cos(deg(pi*x))};
            \end{axis}
            \end{tikzpicture}
            %Here ends the first plot
            \vskip 5pt
            %Here begins the second plot
            \begin{tikzpicture}
            \begin{axis}[
                    width=8cm,height=4cm,
                    ymax = 12, ymin = -12,
                    xmax = 5,
                    axis x line=middle,
                    axis y line=middle, 
                    axis line style={-stealth},
                    xlabel=$t$, xlabel style={at={(1,0.5)}, anchor=west},
                    ylabel=$v$, ylabel style={at={(0,1)}, anchor=south},
                    ytick=\empty,
                    xtick={0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4},
                    xticklabels={, , , , $\tau$, , , , $2\tau$},
            ]
            \addplot[domain=0:4.5, 
                    samples=100, 
                    color=black,
                    thick,]
                {-pi*sin(deg(pi*x))};
            \end{axis}
            \end{tikzpicture}
            \vskip 5pt
            %Here begins the third plot
            \begin{tikzpicture}
            \begin{axis}[
                    width=8cm,height=4cm,
                    ymax = 12, ymin = -12,
                    xmax = 5,
                    axis x line=middle,
                    axis y line=middle, 
                    axis line style={-stealth},
                    xlabel=$t$,xlabel style={at={(1,0.5)}, anchor=west},
                    ylabel=$a$,ylabel style={at={(0,1)}, anchor=south},
                    ytick=\empty,
                    xtick={0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4},
                    xticklabels={, , , , $\tau$, , , , $2\tau$},
            ]
            \addplot[domain=0:4.5, 
                    samples=100, 
                    color=black,
                    thick,]
                {pi^2*cos(deg(pi*x))};
            \end{axis}
            \end{tikzpicture}

PS:我不应该问第二个不相关的问题,但是有没有办法将标签移到每个图形的底部?规范如此要求。

答案1

这是因为您的ylabels 的宽度不同,即x, v, a。每个tikzpicture都将从最左边的位置开始。所有图的宽度并不完全相同!

ylabel如果将所有s 稍微向右移动,则图将正确对齐:

ylabel style={at={(0.05,0.9)}

另一种可能性是将所有图放在一个tikzpicture环境中,然后将每个轴移动到所需的位置:

\begin{axis}[yshift=-3.5cm, ...]

但我不知道这是否能很好地与您的标签配合使用,因为我无法重新创建您的文档。

答案2

这是可编译的、稍微紧凑一些的代码版本。我添加了一些ylabel style选项来为标签提供最小宽度,使其独立于实际字母。我还重新组合了常用轴选项,以澄清代码。

这里的小框仅用于显示使用此选项修改的标签的实际宽度

\documentclass[]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{subfig}
\usepackage{pgfplots}
\usetikzlibrary{arrows, calc,patterns,angles,quotes}
\pgfplotsset{compat=1.9}
\pgfplotsset{commonaxisstyle/.style={
    width=8cm,height=4cm,
    ymax = 12, ymin = -12,
    xmax = 5,
    axis x line=middle,
    axis y line=middle,
    axis line style={-stealth},
    xlabel = {$t$},
    xlabel style={at={(1,0.5)}},
    ylabel style={at={(0,1)}, anchor=south,draw=black,text width=1em,align=center},
    %xtick distance = 90,
    ytick = \empty,
    xtick={0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4},
    xticklabels={ , , , ,$\tau$, , , , $2\tau$},
}}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
            commonaxisstyle,
            ylabel={$x$},
        ]
        \addplot[domain=0:4.5,
            samples=100,
            color=black,
            thick,]
        {cos(deg(pi*x))};
    \end{axis}
\end{tikzpicture}

\begin{tikzpicture}
    \begin{axis}[
        commonaxisstyle,
        ylabel={$v$},
        ]
        \addplot[domain=0:4.5,
            samples=100,
            color=black,
            thick,]
        {-pi*sin(deg(pi*x))};
    \end{axis}
\end{tikzpicture}

\begin{tikzpicture}
    \begin{axis}[
        commonaxisstyle,
        ylabel={$a$},
        ]
        \addplot[domain=0:4.5,
            samples=100,
            color=black,
            thick,]
        {pi^2*cos(deg(pi*x))};
    \end{axis}
\end{tikzpicture}

\end{document}

在此处输入图片描述

答案3

  • 您使用的是旧版本的pgfplots软件包(版本 1.9)。最新版本是 1.17!我强烈建议您升级它。
  • 我猜你喜欢图表的 y 轴对齐,对吗?这可以通过trim left向每个添加选项来实现\tikzpicture
\documentclass[
            % -- opções da classe memoir --
            12pt, openright, oneside, a4paper,
            % -- opções da classe abntex2 --
            chapter=TITLE,  section=TITLE, subsection=Title,
            % -- opções do pacote babel --
            english, french, spanish, portugues,
            %%%%%%%%%%%%
            sumario=tradicional             % título no formato tradicional
            ]{abntex2}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{subcaption}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}   % 1.9 is too ancient
\usetikzlibrary{angles, arrows,
                calc,
                patterns,
                quotes}

\begin{document}
    \begin{figure}
\pgfplotsset{
    width=8cm,height=4cm,
    ymax=15, ymin=-11,
    xmax=5,
    axis lines = middle,
    axis line style={-stealth},
    xlabel = {$t$},
    ylabel style={anchor=south},
    ytick = \empty,
    xtick={0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4},
    xticklabels={ , , , ,$\tau$, , , , $2\tau$},
%
    domain=0:4.5, samples=101,
            }
\tikzset{every picture/.style={trim left}} % <---
\centering
    \subfloat[]{
\begin{tikzpicture}
    \begin{axis}[ylabel={$xxx$}]
\addplot[thick] {cos(deg(pi*x))};
    \end{axis}
\end{tikzpicture}
            }

    \subfloat[]{
\begin{tikzpicture}
    \begin{axis}[ylabel={$v$}]
\addplot[thick] {-pi*sin(deg(pi*x))};
    \end{axis}
\end{tikzpicture}
            }

    \subfloat[]{
\begin{tikzpicture}
    \begin{axis}[ylabel={$a$}]
\addplot[thick]     {pi^2*cos(deg(pi*x))};
    \end{axis}
\end{tikzpicture}
            }
\caption{My caption}
\label{fig:comparison}
    \end{figure}
\end{document}

在此处输入图片描述

笔记:

  • 对于上述 MWE,您需要安装subcaption3.58 或最新版本。
  • pgfplotset在 MWE 中,所有图表都使用来自BambOo 的回答

答案4

谢谢你们!trim稍微移动每个图表并commonstyle 调整字母大小的想法是正确的!这次我设法摆脱了环境subfloat。现在我正在使用subfigures,如下所示:

        [![\documentclass\[\]{article}
        \usepackage\[T1\]{fontenc}
        \usepackage\[utf8\]{inputenc}
        \usepackage{pgfplots}
        \pgfplotsset{compat=1.9}
        \usepackage{caption}        % new package
        \usepackage{subcaption}    % new package
        \begin{document}
        \begin{center}\small
        \begin{figure}\[!ht\]
        \pgfplotsset{commonaxisstyle/.style={
                width=8cm,height=4cm,
                ymax = 12, ymin = -12,
                xmax = 5,
                axis x line=middle,
                axis y line=middle,
                axis line style={-stealth},
                xlabel = {$t$},
                xlabel style={at={(1,0.5)}},
                ylabel style={at={(0,1)}, anchor=south,text width=1em,align=center},
                %xtick distance = 90,
                ytick = \empty,
                xtick={0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4},
                xticklabels={ , , , ,$\tau$, , , , $2\tau$},
                domain=0:4.5, samples=101,}
        \centering
        \tikzset{every picture/.style={trim left}}
        \begin{subfigure}\[b\]{\textwidth}
          \centering
          \begin{tikzpicture}
            \begin{axis}\[commonaxisstyle, ylabel={$x$}\]
            \addplot\[thick\]
            {cos(deg(pi*x))};
            \end{axis}
          \end{tikzpicture}
          \label{position}
       \end{subfigure}
    
    \begin{subfigure}\[b\]{\textwidth}
        \centering
        \begin{tikzpicture}
            \begin{axis}\[commonaxisstyle, ylabel={$v$}\]
            \addplot\[thick\]
            {-pi*sin(deg(pi*x))};
            \end{axis}
        \end{tikzpicture}
        \label{velocity}
    \end{subfigure}
    
    \begin{subfigure}\[b\]{\textwidth}
        \centering
        \begin{tikzpicture}
            \begin{axis}\[commonaxisstyle, ylabel={$a$}\]
            \addplot\[thick\]
            {pi^2*cos(deg(pi*x))};
            \end{axis}
        \end{tikzpicture}
        \label{acceleration}
    \end{subfigure}    
  \end{figure}
  \end{center}
  \end{document}

谢谢!结果如下:[1]:https://i.stack.imgur.com/w9pFt.png

相关内容