[Tex/LaTex] Using pgfplots, add vertical/horizontal spacing between plots

pgfplots

I once already asked a very similar question; How can I add space between two separate plots, using pgfplots?. But this is some what different.

I have the following code;

\documentclass{standalone}
\usepackage{graphics}
\usepackage{tikz,pgfplots}
\pgfplotsset{compat=1.6}
\begin{document}
\begin{tikzpicture}

\begin{axis}[%
width=3cm,
height=3cm,
scale only axis,
every x tick label/.append style={font=\scriptsize\color{gray!80!black}},
xmin=0, xmax=20000,
xlabel={$t$},
xmajorgrids,
xminorgrids,
every y tick label/.append style={font=\scriptsize\color{gray!80!black}},
ymin=0.0004, ymax=0.0012,
ylabel={$\phi_{\mathrm{i}}(t)$},
ymajorgrids,
yminorgrids,
name=plot1]

\end{axis}

\begin{axis}[%
width=3cm,
height=3cm,
scale only axis,
every x tick label/.append style={font=\scriptsize\color{gray!80!black}},
xmin=0, xmax=20000,
xlabel={$t$},
xmajorgrids,
xminorgrids,
every y tick label/.append style={font=\scriptsize\color{gray!80!black}},
ymin=0.2, ymax=1.2,
ylabel={$h(t)$},
ymajorgrids,
yminorgrids,
name=plot2,
at=(plot1.right of north east), anchor=left of north west]

\end{axis}

\begin{axis}[%
width=3cm,
height=3cm,
scale only axis,
every x tick label/.append style={font=\scriptsize\color{gray!80!black}},
xmin=0, xmax=20000,
xlabel={$t$},
xmajorgrids,
xminorgrids,
every y tick label/.append style={font=\scriptsize\color{gray!80!black}},
ymin=0.0004, ymax=0.0012,
ylabel={$\phi_{\mathrm{o}}(t)$},
ymajorgrids,
yminorgrids,
name=plot4,
at=(plot1.below south west), anchor=above north west]

\end{axis}

\begin{axis}[%
width=3cm,
height=3cm,
scale only axis,
every x tick label/.append style={font=\scriptsize\color{gray!80!black}},
xmin=0, xmax=20000,
xlabel={$t$},
xmajorgrids,
xminorgrids,
every y tick label/.append style={font=\scriptsize\color{gray!80!black}},
ymin=2000, ymax=12000,
ylabel={$p_{\mathrm{i}}(t)$},
ymajorgrids,
yminorgrids,
name=plot5,
at=(plot4.right of north east), anchor=left of north west]

\end{axis}

\end{tikzpicture}
\end{document}

This creates the following plot:

Without any spacing using x- yshift.

I think there is to few vertical/horizontal spacing between the plots. I want to increase this. Currently I use x- yshift, this gives the following result:

\documentclass{standalone}
\usepackage{graphics}
\usepackage{tikz,pgfplots}
\pgfplotsset{compat=1.6}
\begin{document}
\begin{tikzpicture}

\begin{axis}[%
width=3cm,
height=3cm,
scale only axis,
every x tick label/.append style={font=\scriptsize\color{gray!80!black}},
xmin=0, xmax=20000,
xlabel={$t$},
xmajorgrids,
xminorgrids,
every y tick label/.append style={font=\scriptsize\color{gray!80!black}},
ymin=0.0004, ymax=0.0012,
ylabel={$\phi_{\mathrm{i}}(t)$},
ymajorgrids,
yminorgrids,
name=plot1]

\end{axis}

\begin{axis}[%
xshift=1cm,
width=3cm,
height=3cm,
scale only axis,
every x tick label/.append style={font=\scriptsize\color{gray!80!black}},
xmin=0, xmax=20000,
xlabel={$t$},
xmajorgrids,
xminorgrids,
every y tick label/.append style={font=\scriptsize\color{gray!80!black}},
ymin=0.2, ymax=1.2,
ylabel={$h(t)$},
ymajorgrids,
yminorgrids,
name=plot2,
at=(plot1.right of north east), anchor=left of north west]

\end{axis}

\begin{axis}[%
yshift=-1cm,
width=3cm,
height=3cm,
scale only axis,
every x tick label/.append style={font=\scriptsize\color{gray!80!black}},
xmin=0, xmax=20000,
xlabel={$t$},
xmajorgrids,
xminorgrids,
every y tick label/.append style={font=\scriptsize\color{gray!80!black}},
ymin=0.0004, ymax=0.0012,
ylabel={$\phi_{\mathrm{o}}(t)$},
ymajorgrids,
yminorgrids,
name=plot4,
at=(plot1.below south west), anchor=above north west]

\end{axis}

\begin{axis}[%
xshift=1cm,
width=3cm,
height=3cm,
scale only axis,
every x tick label/.append style={font=\scriptsize\color{gray!80!black}},
xmin=0, xmax=20000,
xlabel={$t$},
xmajorgrids,
xminorgrids,
every y tick label/.append style={font=\scriptsize\color{gray!80!black}},
ymin=2000, ymax=12000,
ylabel={$p_{\mathrm{i}}(t)$},
ymajorgrids,
yminorgrids,
name=plot5,
at=(plot4.right of north east), anchor=left of north west]

\end{axis}

\end{tikzpicture}
\end{document}

With spacing using x- yshift

I want to know if there is a more consistent manner to do this.

Best Answer

You might want to have a look to the groupplots library (see the documentation 5.5 Grouping plots).

It allows you to deploy graphs in a grid by selecting the group size (e.g. group size= 2 by 2 for your case) or directly the number of rows and columns. Then you can customize the horizontal and vertical distance among the graphs by means of the keys horizontal sep and vertical sep.

In your case, a possible solution is:

\documentclass{standalone}
\usepackage{graphics}
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}
\pgfplotsset{compat=1.6}
\begin{document}
\begin{tikzpicture}
\begin{groupplot}[group style={group size=2 by 2, horizontal sep=2cm, vertical sep=2cm}]

\nextgroupplot[%
width=3cm,
height=3cm,
scale only axis,
every x tick label/.append style={font=\scriptsize\color{gray!80!black}},
xmin=0, xmax=20000,
xlabel={$t$},
xmajorgrids,
xminorgrids,
every y tick label/.append style={font=\scriptsize\color{gray!80!black}},
ymin=0.0004, ymax=0.0012,
ylabel={$\phi_{\mathrm{i}}(t)$},
ymajorgrids,
yminorgrids]
\addplot coordinates{(0,0.0005)(15000,0.001)};

\nextgroupplot[%
width=3cm,
height=3cm,
scale only axis,
every x tick label/.append style={font=\scriptsize\color{gray!80!black}},
xmin=0, xmax=20000,
xlabel={$t$},
xmajorgrids,
xminorgrids,
every y tick label/.append style={font=\scriptsize\color{gray!80!black}},
ymin=0.2, ymax=1.2,
ylabel={$h(t)$},
ymajorgrids,
yminorgrids]
\addplot coordinates{(0,0.5)(15000,1)};

\nextgroupplot[%
width=3cm,
height=3cm,
scale only axis,
every x tick label/.append style={font=\scriptsize\color{gray!80!black}},
xmin=0, xmax=20000,
xlabel={$t$},
xmajorgrids,
xminorgrids,
every y tick label/.append style={font=\scriptsize\color{gray!80!black}},
ymin=0.0004, ymax=0.0012,
ylabel={$\phi_{\mathrm{o}}(t)$},
ymajorgrids,
yminorgrids]
\addplot coordinates{(1000,0.0004)(15000,0.001)};

\nextgroupplot[%
width=3cm,
height=3cm,
scale only axis,
every x tick label/.append style={font=\scriptsize\color{gray!80!black}},
xmin=0, xmax=20000,
xlabel={$t$},
xmajorgrids,
xminorgrids,
every y tick label/.append style={font=\scriptsize\color{gray!80!black}},
ymin=2000, ymax=12000,
ylabel={$p_{\mathrm{i}}(t)$},
ymajorgrids,
yminorgrids]
\addplot coordinates{(5000,2010)(19000,11500)};

\end{groupplot}

\end{tikzpicture}
\end{document}

The result:

enter image description here

Related Question