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'm trying to produce a figure using

\begin{figure}
\centering
\makebox[\columnwidth]{\input{try1.tikz}}
\makebox[\columnwidth]{\input{try2.tikz}}
\end{figure}

The files try1.tikz and try2.tikz are given below. The only difference between them is that in the former xmin=-2, in the latter xmin=-4.

I get

enter image description here

How do I align the panels vertically?

try1.tikz is:

\begin{tikzpicture}[font=\footnotesize\sffamily]

\begin{axis}[%
view={0}{90},
width=4cm,
height=4cm,
scale only axis,
xmin=-2, xmax=1,
ymin=0, ymax=1.7,
name=plot2,
unbounded coords=jump]
\end{axis}

\begin{axis}[
view={0}{90},
width=4cm,
height=4cm,
scale only axis,
xmin=-2, xmax=1,
ymin=0, ymax=1.7,
at=(plot2.left of south west), anchor=right of south east,
unbounded coords=jump]
\end{axis}

\begin{axis}[
view={0}{90},
width=4cm,
height=4cm,
scale only axis,
xmin=-2, xmax=1,
ymin=0, ymax=1.7,
at=(plot2.right of south east), anchor=left of south west,
legend style={nodes=right,font=\tiny},
legend pos= north west,
unbounded coords=jump]

\end{axis}
\end{tikzpicture}

and try2.tikz is:

\begin{tikzpicture}[font=\footnotesize\sffamily]

\begin{axis}[
view={0}{90},
width=4cm,
height=4cm,
scale only axis,
xmin=-4, xmax=1,
ymin=0, ymax=1.7,
name=plot2,
unbounded coords=jump]
\end{axis}

\begin{axis}[
view={0}{90},
width=4cm,
height=4cm,
scale only axis,
xmin=-4, xmax=1,
ymin=0, ymax=1.7,
at=(plot2.left of south west), anchor=right of south east,
unbounded coords=jump]
\end{axis}

\begin{axis}[
view={0}{90},
width=4cm,
height=4cm,
scale only axis,
xmin=-4, xmax=1,
ymin=0, ymax=1.7,
at=(plot2.right of south east), anchor=left of south west,
legend style={nodes=right,font=\tiny},
legend pos= north west,
unbounded coords=jump]
\end{axis}
\end{tikzpicture}
share|improve this question

2 Answers 2

up vote 5 down vote accepted

This brings immediate attention to the groupplots library of pgfplots.

Your plot can be realized through:

% \usepgfplotslibrary{groupplots}
\begin{tikzpicture}
  \begin{groupplot}[group style={rows=2,columns=3},
    view={0}{90},
    width=4cm,height=4cm,
    scale only axis,
    xmin=-2, xmax=1,
    bottom plots/.style={xmin=-4},
    ymin=0, ymax=1.7,unbounded coords=jump]
    \nextgroupplot
    \nextgroupplot
    \nextgroupplot
    \nextgroupplot[bottom plots]
    \nextgroupplot[bottom plots]
    \nextgroupplot[bottom plots]
  \end{groupplot}
\end{tikzpicture}

which will yield the following image: enter image description here

The groupplot environment does the following:

  1. Determines the number of plots by rows and columns in the style group style. (one can also use group size=<cols> by <rows>)
  2. Grab each \nextgroupplot and places it in column-wise order
  3. Each \nextgroupplot is equivalent to \begin{axis} ... \end{axis}
  4. Every option specified in the groupplot (and not in the group style style) environment will be added to each \nextgroupplot macro, thereby each axis environment.

If you need to adjust the spacing between the plots you have the following styles (which should be placed inside the group style key:

  • horizontal sep=<dim>
  • vertical sep=<dim>

Thus you can do:

 group style={vertical sep=0cm}

to make them touch each other in the vertical direction. The pgfplots manual has a whole section of this which I highly suggest you read (it is placed under Related Libraries).

share|improve this answer

You can use the groupplots library:

\documentclass{article}
\usepackage{pgfplots}
\usetikzlibrary[pgfplots.groupplots]

\begin{document}
  \begin{figure}
    \begin{tikzpicture}[font=\footnotesize\sffamily]
      \begin{groupplot}[
          group style={group size=3 by 2},
          view={0}{90},
          width=4cm,
          height=4cm,
          scale only axis,
          xmin=-2, xmax=1,
          ymin=0, ymax=1.7,
          name=plot2,
          unbounded coords=jump]
        ]
        \nextgroupplot          \addplot [domain=-2:1, thick, cyan] {rnd};
        \nextgroupplot          \addplot [domain=-2:1, thick, cyan] {rnd};
        \nextgroupplot          \addplot [domain=-2:1, thick, cyan] {rnd};
        \nextgroupplot[xmin=-4] \addplot [domain=-4:1, thick, cyan] {rnd};
        \nextgroupplot[xmin=-4] \addplot [domain=-4:1, thick, cyan] {rnd};
        \nextgroupplot[xmin=-4] \addplot [domain=-4:1, thick, cyan] {rnd};
      \end{groupplot}
    \end{tikzpicture}
  \end{figure}
\end{document}

Which produces

enter image description here

Any option that is common for all plots can go as the optional arguments to the \begin{groupplot} command, and any that are specific to each plot can be passed to the \nextgroupplot command.

To make the difference between the top and the bottom plots, the best way would probably be by defining a style for all of them, as in zeroth's answer. So I modified my answer to show how options can be passed individually to each \nextgroupplot.

share|improve this answer

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.