1

I am trying to align 4 graphs in a paper. Previous searching led my to the master/slave trick (see code below). Compiling the code as it is gives the desired result, but if I comment the "\hspace{-.35cm}" where I put a "% HERE !!!!!" comment, then the graphs are no more aligned. I obtained the .35cm by trial and error, but I would like a clean solution. Is there any way to do so?

\documentclass[12pt]{article}               

\usepackage[margin=1in,  a4paper, pdftex]{geometry}


\usepackage{pgfplots}

\tikzset{
    master/.style={
        execute at end picture={
            \coordinate (lower right) at (current bounding box.south east);
            \coordinate (upper left) at (current bounding box.north west);
        }
    },
    slave/.style={
        execute at end picture={
            \pgfresetboundingbox
            \path (upper left) rectangle (lower right);
        }
    }
}


\begin{document}

 \begin{figure}%Fig1
  \begin{center}

  FIGURE 

  \begin{tikzpicture}[master]
    \begin{axis}[
      width=2.1in, height=2in,
      ymin=122, ymax=150, ylabel={y label 1},
      name=plot1,
      title={First title}
    ]
    \addplot [color=blue, mark size=3.5pt, only marks, mark=square*, mark options={solid,draw=blue}]
    table[row sep=crcr]{
    0.25 127.29\\
    0.5 145.68\\
    };
    \end{axis}
    \begin{axis}[
      width=2.1in, height=2in,
      xmin=0, xmax=0.5, 
      ymin=166, ymax=190, ylabel={y label 2},
      at=(plot1.right of south east),
      anchor=left of south west,
      title={Second title},
      ]
    \addplot [color=blue, solid, dashed, ultra thick]
    table[row sep=crcr]{
    0.02 170\\
    0.5 180\\
    };
    \end{axis}
  \end{tikzpicture}%


  \title{\textit{Graph B}}

  \begin{tikzpicture}[slave]
    \begin{axis}[
      width=2.1in, height=2in,
      xmin=60, xmax=200, xlabel={x label},
      ymin=-0.08, ymax=0.04, name=plot1, 
      ylabel={y label 3},
      title={3rd title },
      title style={yshift=1ex}
    ]
    \addplot [
    color=blue, solid, ultra thick, forget plot, smooth]
    table[row sep=crcr]{
    60 -1e-5\\
    200 1e-4\\
    };
    \end{axis}

\hspace{-.35cm}     % HERE !!!!!

    \begin{axis}[
      width=2.1in, height=2in,
      xmin=60, xmax=200, xlabel={x label},
      ylabel={y label 4},
      at=(plot1.right of south east),
      anchor=left of south west,
      title={4th title}
    ]
    \addplot [color=blue, solid, ultra thick, forget plot, smooth]
    table[row sep=crcr]{
    60 -2.5e-5\\
    200 0.0001\\
    };
    \end{axis}
    \end{tikzpicture}
  \end{center}
\end{figure}%Fig1

\end{document}
  • Welcome to TeX-SE! This seems like a job for groupplots. – marmot May 5 at 14:15
1

One shouldn't use any \hspace and the like in a tikzpicture environment. There is a special library for situations like yours, groupplots. It can be used as follows.

\documentclass[12pt]{article}               
\usepackage[margin=1in,a4paper,pdftex]{geometry}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\usepgfplotslibrary{groupplots}
\begin{document}
 \begin{figure}%Fig1
  \begin{center} % <-you may want to replace this by \centering
   \begin{tikzpicture}
    \begin{groupplot}[group style={group size=2 by 2,horizontal sep=2cm,vertical
    sep=2.5cm},width=2.1in, height=2in]
     \nextgroupplot[ymin=122, ymax=150, ylabel={y label 1},
       title={First title}]
      \addplot [color=blue, mark size=3.5pt, only marks, mark=square*, mark options={solid,draw=blue}]
      table[row sep=crcr]{
      0.25 127.29\\
      0.5 145.68\\
      };
     \nextgroupplot[xmin=0, xmax=0.5, 
       ymin=166, ymax=190, ylabel={y label 2},
       title={Second title}]
      \addplot [color=blue, solid, dashed, ultra thick]
      table[row sep=crcr]{
      0.02 170\\
      0.5 180\\
      };
     \nextgroupplot[xmin=60, xmax=200, xlabel={x label},
       ymin=-0.08, ymax=0.04,
       ylabel={y label 3},
       title={3rd title}
     ]
     \addplot[color=blue, solid, ultra thick, forget plot, smooth]
     table[row sep=crcr]{
     60 -1e-5\\
     200 1e-4\\
     };
     \nextgroupplot[xmin=60, xmax=200, xlabel={x label},
       ylabel={y label 4},
       title={4th title}
     ]
     \addplot [color=blue, solid, ultra thick, forget plot, smooth]
     table[row sep=crcr]{
     60 -2.5e-5\\
     200 0.0001\\
     };
    \end{groupplot}
    \path  (group c1r1.south east) -- (group c2r2.north west)
    node[midway,font=\large\sffamily]{panel 2};
   \end{tikzpicture}
  \end{center}
 \end{figure}

\end{document}

enter image description here

  • BTW, the reason why 3rd title is shifted is that I copied title style={yshift=1ex}. If you want to unshift it, drop this key. – marmot May 5 at 18:40
  • Thank you. I knew I shouldn't use \hspace, I just could not find another solution. The yshift=1ex shoud not be there, I left it behind when simplifying the original graph. – millo May 5 at 20:22
  • A further question: Would it be possible to add a text line between the two rows (e.g. 'Panel 2') – millo May 5 at 20:46
  • @millo Sure. I added a proposal for that and removed the unwanted shift. – marmot May 5 at 21:10

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.