[Tex/LaTex] MATLAB Plot Alignment in latex

epsMATLAB

I have two plots in which the x-axis are the same and I'd like to have the y-axis positions to be aligned. As the numbering of the y-axes are different when I print them in MATLAB as two eps files, they appear misaligned in the latex (Please see the figure)enter image description here.

Is there any way to align them?

Thanks in advance,
Reza

Best Answer

The problem is almost certainly due to use of the width option for \includegraphics. The following MATLAB code was used to create two EPS files analogous to the originals:

% Answer for "MATLAB Plot Alignment in latex"
% http://tex.stackexchange.com/questions/178745/

clear all; close all;

x=[70 170];
y1=[1 0];
y2=[1e-2 1e100];

figure(1);
semilogy(x,y2);
xlabel('k');
ylabel('Run Time (sec)');
grid on
print -depsc2 q178745-1.eps

figure(2);
h_plot=plot(x,y1);
xlabel('k');
ylabel('Success rate');
grid on
print -depsc2 q178745-2.eps

Examining the two EPS files produced, the regular plot measures 6.87 x 5.44 inches in size, and the semilog plot measures 6.99 x 5.48 inches in size. So it appears that MATLAB is increasing the printed figure size to account for the much wider (and just slightly taller) y axis labels.

You can also visually examine the two files if you can consistently size and position your graphics viewing program windows. Flipping back and forth between the two figures in identical windows, I could see the left margin (to the y axis label) and right margin (to the edge of the axis) were consistent, but the wider semilog labels pushed the left edge of the axis farther away.

Using the following MWE, we see two different behaviors depending on how we align the two files. Using the scale option instead of width, and making a minipage just slightly larger than the largest scaled EPS file lets us align the figures on their right edges, ensuring the axes line up correctly.

\documentclass{article}
\usepackage{graphicx}

\begin{document}
\begin{figure}
\centering
\begin{minipage}{3.5in} % just slightly larger than the scaled EPS size
\hfill \includegraphics[scale=0.5]{q178745-1.eps} % EPS size 6.99 x 5.48 in

\hfill \includegraphics[scale=0.5]{q178745-2.eps}  % EPS size 6.87 x 5.44 in
\end{minipage}
\caption{Two plots, right-aligned and with identical axis size}
\end{figure}

\begin{figure}
\centering
\includegraphics[width=3.5in]{q178745-1.eps} % EPS size 6.99 x 5.48 in

\includegraphics[width=3.5in]{q178745-2.eps}  % EPS size 6.87 x 5.44 in
\caption{Two plots, right-aligned and with identical overall size}
\end{figure}

\end{document}

enter image description here

Related Question