Graphics, Figures & Tablestwo figures vertical align

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
Bahurin
Posts: 5
Joined: Mon Dec 30, 2013 2:33 pm

two figures vertical align

Post by Bahurin »

Hello to all. I have one problem. I prepared document with two independent figures:

Code: Select all

\documentclass[a4paper,12pt]{report} 
\usepackage[T2A]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[english,russian]{babel}
\usepackage{amssymb,amsfonts,amsmath,mathtext,cite,enumerate,float} 
\usepackage[dvips]{graphicx}

\usepackage{color}

\usepackage{geometry} 	
\geometry{left=2cm}		
\geometry{right=1.5cm}	
\geometry{top=1cm}		
\geometry{bottom=2cm}	


\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{width=8cm, height = 6cm}



\begin{document}

\begin{figure}[hr]
\begin{center}
\begin{tikzpicture}
\begin{axis}
\addplot coordinates {
( 338.1, 266.45 )
( 169.1, 143.43 )
( 84.5, 64.80 )
( 42.3, 34.19 )
( 21.1, 9.47 )};
\end{axis}
\end{tikzpicture}
\caption{Figure1}
\end{center}
\end{figure}

\begin{figure}[hr]
\begin{center}
\begin{tikzpicture}
\begin{axis}[ylabel = {$y(x)$}]
\addplot coordinates {
( 338.1, 266.45 )
( 169.1, 143.43 )
( 84.5, 64.80 )
( 42.3, 34.19 )
( 21.1, 9.47 )};
\end{axis}
\end{tikzpicture}
\caption{Figure2}
\end{center}
\end{figure}


\end{document}
but I got vertical shift between figures (see attached figure)
shift.png
shift.png (13.84 KiB) Viewed 9714 times
How can I get vertical align both figures?

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

tom
Posts: 73
Joined: Thu Apr 18, 2013 4:02 am

two figures vertical align

Post by tom »

Hi!

The figures are misaligned because of the labeled axis in the second figure which makes it wider than the first. The easiest solution is to label both y-axes. You can make the label of the first plot invisible if necessary.

Code: Select all

\begin{axis}[ylabel = \phantom{{$y(x)$}}]
...
\end{axis}
Also, you might want to use subcaption/subfig for a better control of the alignment and captions.
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

two figures vertical align

Post by cgnieder »

Hi Bahurin,

Welcome to the LaTeX community!

Probably the easiest here is to add trim axis left as option to the tikzpictures:

Code: Select all

\begin{figure}[h]
  \centering
  \begin{tikzpicture}[trim axis left]
    \begin{axis}
      \addplot coordinates {
        ( 338.1, 266.45 )
        ( 169.1, 143.43 )
        ( 84.5, 64.80 )
        ( 42.3, 34.19 )
        ( 21.1, 9.47 )
      };
    \end{axis}
  \end{tikzpicture}
  \caption{Figure1}
\end{figure}

\begin{figure}[h]
  \centering
  \begin{tikzpicture}[trim axis left]
    \begin{axis}[ylabel = {$y(x)$}]
      \addplot coordinates {
        ( 338.1, 266.45 )
        ( 169.1, 143.43 )
        ( 84.5, 64.80 )
        ( 42.3, 34.19 )
        ( 21.1, 9.47 )
      };
    \end{axis}
  \end{tikzpicture}
  \caption{Figure2}
\end{figure}
A few additional remarks:
  • There is no float specifier r...
  • I wouldn't use the {center} environment inside of the {figure} environment since it adds additional verticale whitespace above and below. \centering will work fine instead.
  • \usepackage[dvips]{graphicx} it's better to leave the dvips option out and let graphicx figure out the needed driver itself.
  • \usepackage{color} this package is superseded by xcolor. Since the TikZ package already loads xcolor you can remove that line completely
Regards
site moderator & package author
Bahurin
Posts: 5
Joined: Mon Dec 30, 2013 2:33 pm

two figures vertical align

Post by Bahurin »

cgnieder wrote:Hi Bahurin,

Welcome to the LaTeX community!

Probably the easiest here is to add trim axis left as option to the tikzpictures
Thank you very much for your advices. It is working correct now, but I have additional question.
Y label font decreases if I set ylabel as fraction. For example
\begin{axis}[ylabel = {$\frac{y(x)}{z(x)}$}]
How can I set normal font size for ylabel and keep vertical figures align in the same time?

2) Figures are not visible if I remove \usepackage{color}.
Post Reply