如何对齐几个不同大小的轴?

如何对齐几个不同大小的轴?

对于不同轴的排列,pgfplots 文档和示例中提到了几种选项。大多数仅考虑所有轴具有相同大小的情况。

如何排列和缩放不同大小的轴?

这里,右侧的轴应该与左上角图的顶部对齐(这样才有效),底部应该与左下角图的底部对齐(这样才无效):

轴未对准

\documentclass{standalone}

\usepackage{pgfplots}
\pgfplotsset{compat=1.18} 
\usetikzlibrary{calc}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    name=left plot,
    width=4cm,
    scale only axis,
    ylabel=a,
    ]
    \addplot {x^2};
\end{axis}

\begin{axis}[
    name=bottom plot,
    at={(left plot.below south west)},
    anchor=north west,
    width=4cm,
    scale only axis,
    ylabel=b,
    ]
    \addplot {x^2};
\end{axis}

\begin{axis}[
    name=right plot,
    at={($(left plot.north east) + (1.5cm,0)$)},
    anchor=north west,
    width=4cm,
    height=8cm, % which height is appropriate here?
    scale only axis,
    ylabel=c,
    ]
    \addplot {x^2};
\end{axis}
\end{tikzpicture}
\end{document}

答案1

要做到这一点,您“仅”需要知道height用于“左”图的 s 和图的距离(没有标签的轴边框,即黑框)...

希望代码不言自明。如果不是,请随时询问,我会添加更多评论。

% used PGFPlots v1.18.1
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
    \usetikzlibrary{calc}
    % use this `compat` level or higher to make use of the advanced axis label positioning
    \pgfplotsset{compat=1.3}
\begin{document}
\begin{tikzpicture}[
    % state common `axis` options here
    % (so we don't need to repeat ourselfes)
    /pgfplots/.cd,
        width=4cm,
        height=\Height,
        % (luckily you already had implemented this key, which makes it a bit
        %  easier to calculate the needed `height`)
        scale only axis,
    /tikz/.cd,
]
        \pgfmathsetlengthmacro{\Height}{35mm}
%        % or, if you don't state a `height` explicitly use the default value
%        \pgfmathsetlengthmacro{\Height}{\axisdefaultheight}
        \pgfmathsetlengthmacro{\xOffset}{15mm}
        \pgfmathsetlengthmacro{\yOffset}{10mm}
    \begin{axis}[
        name=left plot,
        ylabel=a,
    ]
        \addplot {x^2};
    \end{axis}

    \begin{axis}[
        name=bottom plot,
        at={($ (left plot.south west) + (0,-\yOffset) $)},
        anchor=north west,
        ylabel=b,
    ]
        \addplot {x^2};
    \end{axis}

    \begin{axis}[
        name=right plot,
        at={($ (left plot.north east) + (\xOffset,0) $)},
        anchor=north west,
        height={2*\Height + \yOffset},
        ylabel=c,
    ]
        \addplot {x^2};
    \end{axis}
\end{tikzpicture}
\end{document}

该图显示了上述代码的结果

相关内容