# Grouping of the multiple axis plots in LaTeX

It is quite usual in scientific texts that one needs to places several graphs next to each other for a better comparison. In general this leads to a “matrix style” placement of the graphs. Let us discuss the ways how to get it in LaTeX.

In general case, when the graphs are *,eps files (e.g. exported from Origin, Qtiplot or similar program), one achieves the proper placement by:

\usepackage{graphicx}

\begin{figure}
\includegraphics[width=0.5\linewidth]{“”}
\includegraphics[width=0.5\linewidth]{“”}
~
\includegraphics[width=0.5\linewidth]{“”}
\includegraphics[width=0.5\linewidth]{“”}
\end{figure}

This way seems to be the easiest, however it shares all the disadvantages, that embedding of the external pictures has:

• possibly “foreign” fonts with wrong size,
• the sizes and axes of the graphs should be properly chosen,
• more difficult to maintain certain “style” among several graphs,

Using TikZ/PGF package solves these problems, however it is mandatory, that the used data is already “prepared” for visualizing in external program.

In the following examples I use PGFplots, package built on top of PGF for easier plotting.

Something like:
\usepackage{pgfplots}

\begin{tikzpicture}
\begin{axis}
\addplot coordinates {(0,2) (2,0)}; %Or, whatever plot you want
\end{axis}
\end{tikzpicture}

is enough to plot you wonderful data…

However, the consequent usage of axis environment creates overlapping plots, what can be used for multi-axes plots with different abscissae and ordinates:

\begin{tikzpicture}
\begin{axis}[xmin=0,xmax=2,ymin=0,ymax=2,
axis y line*=left,
axis x line*=top,]
\end{axis}
\begin{axis}[xmin=0,xmax=4,ymin=0,ymax=4,
axis y line*=right,
axis x line*=bottom,]
\end{axis}
\end{tikzpicture}

To produce a picture with grouped plots one could name the axis environments and grouping them as nodes:

\begin{tikzpicture}
\begin{axis}[name=plot1]
\end{axis}
\begin{axis}[name=plot2,,at={($(plot1.east)+(1cm,0)$)},anchor=west]
\end{axis}
\end{tikzpicture}

However, there is a library, called groupplots, that can automate a bit the grouping procedure and reduce amount of code. The following is a previous block of code rewritten with usage of this library:

\usepgfplotslibrary{groupplots}

\begin{tikzpicture}
\begin{groupplot}[group style={columns=2,rows=1},]
\nextgroupplot % 1
\nextgroupplot % 2
\end{groupplot}
\end{tikzpicture}

where columns times rows defines maximum number of plots, so define it properly.

The caveat is that there is no direct way to alter automatic plot positioning safe for \nextgroupplot[group/empty plot], which effectively skips this particular plot position.
However, one can have workaround for this issue.
Each \nextgroupplot inside current tikzpicture environment can be references as a node, e.g. :
\draw[thick,>=latex,->,red] (group c1r1.center) node {1.} — (group c2r1.center) node {2.};
Will connect center of the plots with an arrow.

Thus we come to he most important part of the whole text, 2 plots, each of them with 2 different axes:

\begin{tikzpicture}
\begin{groupplot}[group style={columns=2,rows=2},]
\nextgroupplot[axis y line*=left,axis x line*=bottom,] % 1
\nextgroupplot[axis y line*=left,axis x line*=bottom,] % 2
\nextgroupplot[at=(group c1r1.north),axis y line*=right,axis x line*=top,] % 1, 2nd axis
\nextgroupplot[at=(group c2r1.north),axis y line*=right,axis x line*=top] % 2, 2nd axis
\end{groupplot}
\draw[thick,,red,shorten >=4pt,shorten <=4pt]
(group c1r1.center) node {1.} —
(group c2r1.center) node {2.};
\end{tikzpicture}