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

  • use \hspace*{2mm}\includegraphics{...} or whatever you need to push it to the right place – David Carlisle May 17 '14 at 15:38
  • \includegraphics also accepts a trim=<left> <below> <right> <top> option which allows to exclude parts of the bounding box. You could exclude the left stuff. – Christian Feuersänger May 17 '14 at 18:23
  • You may also be interested in mathworks.com/matlabcentral/fileexchange/22022-matlab2tikz for the individual plots combined with \begin{axis}[trim axis left] which does the same automatically – Christian Feuersänger May 17 '14 at 18:24
  • Even if you trimmed or offset a graphic, you'd also need to scale (since the total width of the graphics are constant, and the y axis labels are different). You could use the get function to extract the position and size of the run time axes, and the set function to apply that position and size to the success rate axes. – Mike Renfro May 17 '14 at 23:28
  • @MikeRenfro Perhaps you could write an answer. – Torbjørn T. Aug 2 '14 at 22:44

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];

ylabel('Run Time (sec)');
grid on
print -depsc2 q178745-1.eps

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.


\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
\caption{Two plots, right-aligned and with identical axis size}

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


enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.