如何将组图的边界框设置为组图中子图的左外轴和右外轴?
我的 MWE 中有三个图,我想将组图的外轴与示例中的第一个图对齐。
组图的宽度总和始终为 \textwidth,作为第一个图的边界框的宽度。
\documentclass{article}
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}
\begin{document}
\setlength{\fboxsep}{0pt}%
\fbox{
\begin{tikzpicture}
\begin{pgfinterruptboundingbox}
\begin{axis} [xlabel=x, ylabel=y, height=3.7cm,width=\textwidth]
\addplot {rnd};
\end{axis}
\end{pgfinterruptboundingbox}
\useasboundingbox
(current axis.below south west)
rectangle (current axis.above north east);
\end{tikzpicture}
}
\setlength{\fboxsep}{0pt}
\fbox{\begin{tikzpicture}
\begin{groupplot}[group style={group size=2 by 2,
horizontal sep=0.02\textwidth, vertical sep=0.02\textwidth,
xticklabels at=edge bottom},
height=3.7cm,width=0.49\textwidth]
\nextgroupplot[xlabel=x, ylabel=y, ylabel near ticks]
\addplot {rnd};
\nextgroupplot[xlabel=x, ylabel=y,yticklabel pos=right, ylabel near ticks]
\addplot {rnd};
\nextgroupplot[xlabel=x, ylabel=y]
\addplot {rnd};
\nextgroupplot[xlabel=x, ylabel=y,yticklabel pos=right, ylabel near ticks]
\addplot {rnd};
\end{groupplot}
\end{tikzpicture}}
\setlength{\fboxsep}{0pt}
\fbox{\begin{tikzpicture}
\begin{groupplot}[group style={group size=3 by 2,
horizontal sep=0.02\textwidth, vertical sep=0.02\textwidth,
xticklabels at=edge bottom},
height=3.7cm,width=0.32\textwidth]
\nextgroupplot[xlabel=x, ylabel=y, ylabel near ticks]
\addplot {rnd};
\nextgroupplot[ytick=\empty]
\addplot {rnd};
\nextgroupplot[xlabel=x, ylabel=y,yticklabel pos=right, ylabel near ticks]
\addplot {rnd};
\nextgroupplot[xlabel=x, ylabel=y]
\addplot {rnd};
\nextgroupplot[ytick=\empty]
\addplot {rnd};
\nextgroupplot[xlabel=x, ylabel=y,yticklabel pos=right, ylabel near ticks]
\addplot {rnd};
\end{groupplot}
\end{tikzpicture}}
\end{document}
答案1
我自己找到了一个解决方案。使用矩阵结合每个图的边界框设置可以实现正确的对齐:
\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\matrix[] {
\begin{axis} [xlabel=x, ylabel=y, height=2cm,width=0.5\textwidth, scale only axis]
\addplot {rnd};
\end{axis}
\pgfresetboundingbox
\path
(current axis.south west)
rectangle (current axis.north east);
\\};
\end{tikzpicture}
\vspace{2em}
\begin{tikzpicture}
\matrix[row sep=0.01\textwidth, column sep=0.01\textwidth] {
\begin{axis}[xtick=\empty,height=2cm,width=0.245\textwidth, scale only axis]
\addplot {rnd};
\end{axis}
\pgfresetboundingbox
\path
(current axis.south west)
rectangle (current axis.north east);
&
\begin{axis}[xtick=\empty,yticklabel pos=right,height=2cm,width=0.245\textwidth, scale only axis]
\addplot {rnd};
\end{axis}
\pgfresetboundingbox
\path
(current axis.south west)
rectangle (current axis.north east);
\\
\begin{axis}[height=2cm,width=0.245\textwidth,scale only axis]
\addplot {rnd};
\end{axis}
\pgfresetboundingbox
\path
(current axis.south west)
rectangle (current axis.north east);
&
\begin{axis}[yticklabel pos=right,height=2cm,width=0.245\textwidth, scale only axis]
\addplot {rnd};
\end{axis}
\pgfresetboundingbox
\path
(current axis.south west)
rectangle (current axis.north east);
\\};
\end{tikzpicture}
\end{document}