2

I have a number of plots obtained with matlab2tikz. Now I want to align these plot in a neat way. I used the subfigure package, but the result is not aligned very nice, see

enter image description here

My latex code is:

 \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}

How can I achieve that the vertical axis of the plots are aligned nicely? Thanks

4
  • what about creating the subfigures already in Matlab, and use the xlabel as subcaption? I think the subfigures should then be properly aligned.
    – bene
    Oct 10, 2014 at 15:51
  • @bene if the user doesn't know how to do that (use subplots in Matlab), this question becomes a SO question then not a LaTeX question.
    – dustin
    Oct 10, 2014 at 15:53
  • If the problem are left tics, you can put subfigures inside a right aligned tabular.
    – Ignasi
    Oct 10, 2014 at 17:28
  • I solved this question with help of Array Alignment using TikZ Matrices, see page 320 of the pgfplots reference. With many thanks to the answer of bene which helped me in the right direction
    – Derk
    Oct 16, 2014 at 11:19

2 Answers 2

4

The problem are the different tick formats of your axis. Here is an example with only 2x2 where it is solved.

First of all, I created a 2x2 figure in Matlab using subplot(2,2,x) with x from 1 to 4. I exported the figure to tikz and got the following code:

% 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}%

Insert this in your latex-file and you will see, there is still a misalignment. What I did then is to change the order of the individual suplots in the tikz-file. You can have a look at page 314 of the current pgfplots documentation for the definition of anchors.

I came up with this here:

% 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}%

If you insert this into your latex-file, you will see a nice alignment.

2

Why TikZ? You could just save the figures from Matlab as an eps file and use the package 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}

With Matlab, you may need to do

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

for the first figure. The reason is Matlab adds white space. Once you determine the trim amounts, you can remove \fbox{...} and add trim = {what you determine}, clip to other figures.

4
  • 1
    There are reasons to use TikZ. I would not recommend someone who wants TikZ to use eps.
    – bene
    Oct 10, 2014 at 15:49
  • @bene both the TikZ and vector graphics of the eps file will have the same clarity so we aren't sacrificing anything in that department.
    – dustin
    Oct 10, 2014 at 15:53
  • 2
    I know, but with TikZ you have the same font properties as the rest of the document. That is something which I personally don't want to miss anymore.
    – bene
    Oct 10, 2014 at 15:54
  • @bene I am aware. I generally write my data to a csv file and use pgfplots for it.
    – dustin
    Oct 10, 2014 at 15:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .