Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question

2 Answers 2

Since matrix alignment is unsuitable, you can still rely on standard LaTeX box alignment combined with \centering. That would automatically align your images - and it would automatically ensure that the last line is centered independently.

In fact, this is considerably simpler in terms of writing and maintenance of the code, and it also produces individual, independent images (which is something that I always like compared to a single, huge, involved picture).

Here is what I think about, applied to your example (see below for explanation):

\documentclass{article}
\usepackage[left=2cm,right=2cm]{geometry}
\usepackage{graphics}
\usepackage{tikz,pgfplots}
\pgfplotsset{compat=1.6}
\begin{document}
\thispagestyle{empty}

\centering

\tikzset{trim axis left,trim axis right}
\def\distance{2cm}%

\dotfill
\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}
\end{tikzpicture}%
\hspace{\distance}%
\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}
\end{tikzpicture}%
\hspace{\distance}%
%
\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.2, ymax=1.2,
ylabel={$h(t)$},
ymajorgrids,
yminorgrids,
name=plot2,
]

\end{axis}
\end{tikzpicture}%
\dotfill
\vspace{1.2cm}%

\dotfill
%
\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{o}}(t)$},
ymajorgrids,
yminorgrids,
name=plot4,
]

\end{axis}
\end{tikzpicture}%
\hspace{\distance}%
%
\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=2000, ymax=12000,
ylabel={$p_{\mathrm{i}}(t)$},
ymajorgrids,
yminorgrids,
name=plot5,
]

\end{axis}

\end{tikzpicture}%
\dotfill

\end{document}

enter image description here

The key idea is to generate five individual pictures. The first three pictures are separated by \hspace{\distance} which produces the desired distance. You see that I defined \distance to be 2cm.

Then, the line is terminated using a new paragraph. The second line contains two pictures which are separated by \hspace{\distance} as well. In addition, the vertical distance is defined by a \vspace.

Since the complete stuff has been typeset after \centering, the lines will be centered individually (in fact, the \centering is redundant in my example because of the \dotfills. But I suppose that you will remove them anyway, see below).

There is one challenge in the alignment: it can easily happen that axis descriptions have different sizes. However, the eye always gets caught by the axis box - so we should align on the axis box, even if the tick labels or axis labels would result in a different alignment. Consequently, I wrote the line

\tikzset{trim axis left,trim axis right}

which applies to all following pictures (until the next closing brace or \end{...}). This statement causes TikZ to exclude everything left of the axis and everything right of the axis. This requires manual control over the spacing. Furthermore, it can lead to axis descriptions in the side's margin. But I believe that it is still a tremendiously useful feature. The \dotfills show the result: they indicate the end of the bounding boxes on the left and on the right. You can remove them, they are just for the purpose of illustration.

Note that you could write a \pgfplotsset{<common options>} in front of your pictures (or using a common style key). These options will apply to all following pictures until the next closing curly brace or \end{...}.

Personally, I find this kind of alignment quite appealing because you can easily copy-paste individual pictures to other documents (well, together with the shared set of options) and because it lets LaTeX do the alignment job. The same approach could be combined with standard LaTeX tabulars - just in the same way as for \includegraphics, except that the trim left, trim right allows a powerful difference.

Ah - you may want to add baseline to the first \tikzset. It will automatically control the vertical alignment of adjacent pictures.

share|improve this answer
    
How can I use your code using the standalone package? When I use your code the plots are all plotted on the same line. Using the 'varwidth' option of the standalone package did not help either. –  WG- Dec 16 '12 at 20:54
    
@WG- Start the file via \documentclass[preview]{standalone} \usepackage[left=2cm,right=2cm]{geometry} \usepackage{graphicx} instead. –  percusse Dec 17 '12 at 2:09

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

share|improve this answer
    
Thank you for your answer. I however do not want to use groupplot. The problem lays in when I want to plot a uneven number of plots. You then get, for example, 3 plots on the first row and 2 on the second row in matrix style. The problem is that this is ugly, I want the 2 plots on the second row at that moment centered compared to the first row. –  WG- Dec 14 '12 at 12:08
2  
@WG- You can use two sets of groupplots and place them relatively. –  percusse Dec 14 '12 at 12:14
    
I haven't thought of that. But it's a lot of work for something this small. I hope a future release of pgfplots maybe can do this all automatic. I.e. in the groupplot you can set the space distance (already available) but when you make a groupplot of 2x3 and leave one entry open that that it centers automatically. For now I think I will stick with my own solution and just use xshift and yshift. –  WG- Dec 14 '12 at 12:22

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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