使用 tikz 将子图对齐到 2x3 网格中

使用 tikz 将子图对齐到 2x3 网格中

我使用 matlab2tikz 获得了一些图。现在我想以一种整齐的方式对齐这些图。我使用了 subfigure 包,但结果对齐得不是很好,请参见

在此处输入图片描述

我的乳胶代码是:

 \begin{figure}
\begin{subfigure}[b]{0.3\linewidth}
  \centering
  \setlength\figureheight{0.6\textwidth}
  \setlength\figurewidth{1.0\textwidth}
  \input{figures/mse_xdot.tikz}
  \caption{$\dot{x}$}
  \label{fig:mse_xdot}
\end{subfigure}\hfill
\begin{subfigure}[b]{.3\linewidth}
  \centering
  \setlength\figureheight{0.6\textwidth}
  \setlength\figurewidth{1.0\textwidth}
  \input{figures/mse_ydot.tikz}
  \caption{$\dot{y}$}
  \label{fig:mse_ydot}
\end{subfigure}\hfill
\begin{subfigure}[b]{.3\linewidth}
  \centering
  \setlength\figureheight{0.6\textwidth}
  \setlength\figurewidth{1.0\textwidth}
  \input{figures/mse_omegadot.tikz}
  \caption{$\dot{\omega}$}
  \label{fig:mse_omegadot}
\end{subfigure}\\
\begin{subfigure}[b]{.3\linewidth}
  \centering
  \setlength\figureheight{0.6\textwidth}
  \setlength\figurewidth{1.0\textwidth}
  \input{figures/mse_p.tikz}
  \caption{$p$}
  \label{fig:mse_p}
\end{subfigure}\hfill
\begin{subfigure}[b]{.3\linewidth}
  \centering
  \setlength\figureheight{0.6\textwidth}
  \setlength\figurewidth{1.0\textwidth}
  \input{figures/mse_q.tikz}
  \caption{$q$}
  \label{fig:mse_q}
\end{subfigure}\hfill
\begin{subfigure}[b]{.3\linewidth}
  \centering
  \setlength\figureheight{0.6\textwidth}
  \setlength\figurewidth{1.0\textwidth}
  \input{figures/mse_alpha.tikz}
  \caption{$\alpha$}
  \label{fig:mse_alpha}
\end{subfigure}
\caption{Mean-squared error results up to $10$ simulation steps. The time is plotted on the horizontal axis in seconds versus the mean-squared error on the vertical axis.}
\end{figure}

我怎样才能使图的垂直轴完美对齐?谢谢

答案1

问题在于轴的刻度格式不同。下面是一个只有 2x2 的示例,它解决了这个问题。

首先,我在 Matlab 中使用 subplot(2,2,x) 创建了一个 2x2 图形,其中 x 从 1 到 4。我将图形导出到 tikz 并得到以下代码:

% This file was created by matlab2tikz v0.4.6 running on MATLAB 8.3.
% Copyright (c) 2008--2014, Nico Schlömer <[email protected]>
% All rights reserved.
% Minimal pgfplots version: 1.3
% 
\begin{tikzpicture}

\begin{axis}[%
width=\figwidth,
height=\figheight,
scale only axis,
xmin=-1,
xmax=2,
xlabel={(a)},
ymin=-0.005,
ymax=2,
name=plot1
]
\addplot [color=blue,solid,forget plot]
  table[row sep=crcr]{
1   1   \\
};
\end{axis}

\begin{axis}[%
width=\figwidth,
height=\figheight,
scale only axis,
xmin=0,
xmax=2,
xlabel={(b)},
ymin=0.5,
ymax=2,
name=plot2,
at=(plot1.right of south east),
anchor=left of south west
]
\addplot [color=blue,solid,forget plot]
  table[row sep=crcr]{
1   1   \\
};
\end{axis}

\begin{axis}[%
width=\figwidth,
height=\figheight,
scale only axis,
xmin=0,
xmax=100,
xlabel={(d)},
ymin=0,
ymax=100,
name=plot4,
at=(plot2.below south west),
anchor=above north west
]
\addplot [color=blue,solid,forget plot]
  table[row sep=crcr]{
1   1   \\
};
\end{axis}

\begin{axis}[%
width=\figwidth,
height=\figheight,
scale only axis,
xmin=-10,
xmax=10,
xlabel={(c)},
ymin=-1,
ymax=2,
at=(plot4.left of south west),
anchor=right of south east
]
\addplot [color=blue,solid,forget plot]
  table[row sep=crcr]{
1   1   \\
};
\end{axis}
\end{tikzpicture}%

将其插入到您的 latex 文件中,您会看到,仍然有错位。然后我所做的是更改 tikz 文件中各个 suplots 的顺序。您可以查看当前 pgfplots 文档的第 314 页以了解锚点的定义。

我想到了这个:

% This file was created by matlab2tikz v0.4.6 running on MATLAB 8.3.
% Copyright (c) 2008--2014, Nico Schlömer <[email protected]>
% All rights reserved.
% Minimal pgfplots version: 1.3
% 
\begin{tikzpicture}

\begin{axis}[%
width=\figwidth,
height=\figheight,
scale only axis,
xmin=-1,
xmax=2,
xlabel={(a)},
ymin=-0.005,
ymax=2,
name=plot1
]
\addplot [color=blue,solid,forget plot]
  table[row sep=crcr]{
1   1   \\
};
\end{axis}


\begin{axis}[%
width=\figwidth,
height=\figheight,
scale only axis,
xmin=-10,
xmax=10,
xlabel={(c)},
ymin=-1,
ymax=2,
name=plot2,
at=(plot1.below south west),
anchor=above north west
]
\addplot [color=blue,solid,forget plot]
  table[row sep=crcr]{
1   1   \\
};
\end{axis}

\begin{axis}[%
width=\figwidth,
height=\figheight,
scale only axis,
xmin=0,
xmax=100,
xlabel={(d)},
ymin=0,
ymax=100,
name=plot3,
at=(plot2.right of south east),
anchor=left of south west
]
\addplot [color=blue,solid,forget plot]
  table[row sep=crcr]{
1   1   \\
};
\end{axis}


\begin{axis}[%
width=\figwidth,
height=\figheight,
scale only axis,
xmin=0,
xmax=2,
xlabel={(b)},
ymin=0.5,
ymax=2,
at=(plot3.above north west),
anchor=below south west
]
\addplot [color=blue,solid,forget plot]
  table[row sep=crcr]{
1   1   \\
};
\end{axis}



\end{tikzpicture}%

如果你将其插入到你的 latex 文件中,你会看到一个很好的对齐。

答案2

为什么选择 TikZ?您可以将 Matlab 中的图形保存为 eps 文件并使用该包subcaption

\documentclass{article}
\usepackage[demo]{graphicx}
\usepackage{subcaption}%  http://ctan.org/pkg/subcaption                       
\captionsetup[subfigure]{labelformat = parens, labelsep = space, font = small}
\begin{document}
\begin{figure}
  \centering
  \subcaptionbox{your caption\label{1}}{\includegraphics[width = 1.25in]{figure1}}\quad
  \subcaptionbox{your caption\label{2}}{\includegraphics[width = 1.25in]{figure2}}\quad
  \subcaptionbox{your caption\label{3}}{\includegraphics[width = 1.25in]{figure3}}\\
  \subcaptionbox{your caption\label{4}}{\includegraphics[width = 1.25in]{figure4}}\quad
  \subcaptionbox{your caption\label{5}}{\includegraphics[width = 1.25in]{figure5}}\quad
  \subcaptionbox{your caption\label{6}}{\includegraphics[width = 1.25in]{figure6}}
  \caption{Caption}
  \label{all}
\end{figure}
\end{document}

使用 Matlab,你可能需要做

\fbox{\includegraphics[width = 1.25in, trim = {0 0 0 0}, clip]{figure5}}

对于第一个图形。原因是 Matlab 添加了空白。一旦确定了修剪量,您就可以删除\fbox{...}并添加trim = {what you determine}, clip到其他图形中。

相关内容