0

I hope to align multiple figures plotted by tikzpictures.

As shown in the below codes, I draw eight figures, they are automatically shown in 4 rows and 2 columns, but I want them to be in 2 rows and 4 columns. Are there related methods to align them as we want, for example in 2 rows and 4 columns in this example?

Besides, I want to have them in one single PDF file and with no or narrow borders, so I use {standalone} for \documentclass.

Thank you very much

\documentclass[varwidth]{standalone}%border=3mm, 
\usepackage{tikz}
\usepackage{subfigure}
\usepackage{pgfplotstable}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{units}

\begin{document}

\pgfplotsset{width=5cm,height=5cm,grid style={dashed}}
\begin{filecontents}{plottedtata.txt}
x    y1        y2        y3        y4        y5        y6        y7        y8
x    m         m         m         m         m         m         m         m
1    0.6951    0.6678    0.6753    0.9160    0.4609    0.4714    0.4735    0.1917
2    0.0680    0.8444    0.0067    0.0012    0.7702    0.0358    0.1527    0.7384
3    0.2548    0.3445    0.6022    0.4624    0.3225    0.1759    0.3411    0.2428
4    0.2240    0.7805    0.3868    0.4243    0.7847    0.7218    0.6074    0.9174
\end{filecontents}

\pgfplotstableread[
  skip first n=6, % 2+4 for filecontents header
]{plottedtata.txt}{\plottedtata}
\begin{figure}
\begin{tikzpicture}
    \begin{axis}
        [color=black,grid=major,use units, y unit=m,xlabel=(1), ylabel=y,
        every major tick/.append style={thick, major tick length=3pt, black},
        every minor tick/.append style={thick, minor tick length=1.7pt, black},
        xmin=1, xmax=4, xtick={1,2,3,4}, tick align=outside, minor x tick num=1,
        ymin=0, ymax=1, minor y tick num=1]
        \addplot[draw=red,line width=1,mark=*,mark options={fill=red}] table [x index=0,y index=1,]{\plottedtata};
    \end{axis}
\end{tikzpicture}
\begin{tikzpicture}
    \begin{axis}
        [color=black,grid=major,use units, y unit=m,xlabel=(2), ylabel=y,
        every major tick/.append style={thick, major tick length=3pt, black},
        every minor tick/.append style={thick, minor tick length=1.7pt, black},
        xmin=1, xmax=4, xtick={1,2,3,4}, tick align=outside, minor x tick num=1,
        ymin=0, ymax=1, minor y tick num=1]
        \addplot[draw=red,line width=1,mark=*,mark options={fill=red}] table [x index=0,y index=2,]{\plottedtata};
    \end{axis}
\end{tikzpicture}
\begin{tikzpicture}
    \begin{axis}
        [color=black,grid=major,use units, y unit=m,xlabel=(3), ylabel=y,
        every major tick/.append style={thick, major tick length=3pt, black},
        every minor tick/.append style={thick, minor tick length=1.7pt, black},
        xmin=1, xmax=4, xtick={1,2,3,4}, tick align=outside, minor x tick num=1,
        ymin=0, ymax=1, minor y tick num=1]
        \addplot[draw=red,line width=1,mark=*,mark options={fill=red}] table [x index=0,y index=3,]{\plottedtata};
    \end{axis}
\end{tikzpicture}
\begin{tikzpicture}
    \begin{axis}
        [color=black,grid=major,use units, y unit=m,xlabel=(4), ylabel=y,
        every major tick/.append style={thick, major tick length=3pt, black},
        every minor tick/.append style={thick, minor tick length=1.7pt, black},
        xmin=1, xmax=4, xtick={1,2,3,4}, tick align=outside, minor x tick num=1,
        ymin=0, ymax=1, minor y tick num=1]
        \addplot[draw=red,line width=1,mark=*,mark options={fill=red}] table [x index=0,y index=4,]{\plottedtata};
    \end{axis}
\end{tikzpicture}
\begin{tikzpicture}
    \begin{axis}
        [color=black,grid=major,use units, y unit=m,xlabel=(5), ylabel=y,
        every major tick/.append style={thick, major tick length=3pt, black},
        every minor tick/.append style={thick, minor tick length=1.7pt, black},
        xmin=1, xmax=4, xtick={1,2,3,4}, tick align=outside, minor x tick num=1,
        ymin=0, ymax=1, minor y tick num=1]
        \addplot[draw=red,line width=1,mark=*,mark options={fill=red}] table [x index=0,y index=5,]{\plottedtata};
    \end{axis}
\end{tikzpicture}
\begin{tikzpicture}
    \begin{axis}
        [color=black,grid=major,use units, y unit=m,xlabel=(6), ylabel=y,
        every major tick/.append style={thick, major tick length=3pt, black},
        every minor tick/.append style={thick, minor tick length=1.7pt, black},
        xmin=1, xmax=4, xtick={1,2,3,4}, tick align=outside, minor x tick num=1,
        ymin=0, ymax=1, minor y tick num=1]
        \addplot[draw=red,line width=1,mark=*,mark options={fill=red}] table [x index=0,y index=6,]{\plottedtata};
    \end{axis}
\end{tikzpicture}
\begin{tikzpicture}
    \begin{axis}
        [color=black,grid=major,use units, y unit=m,xlabel=(7), ylabel=y,
        every major tick/.append style={thick, major tick length=3pt, black},
        every minor tick/.append style={thick, minor tick length=1.7pt, black},
        xmin=1, xmax=4, xtick={1,2,3,4}, tick align=outside, minor x tick num=1,
        ymin=0, ymax=1, minor y tick num=1]
        \addplot[draw=red,line width=1,mark=*,mark options={fill=red}] table [x index=0,y index=7,]{\plottedtata};
    \end{axis}
\end{tikzpicture}
\begin{tikzpicture}
    \begin{axis}
        [color=black,grid=major,use units, y unit=m,xlabel=(8), ylabel=y,
        every major tick/.append style={thick, major tick length=3pt, black},
        every minor tick/.append style={thick, minor tick length=1.7pt, black},
        xmin=1, xmax=4, xtick={1,2,3,4}, tick align=outside, minor x tick num=1,
        ymin=0, ymax=1, minor y tick num=1]
        \addplot[draw=red,line width=1,mark=*,mark options={fill=red}] table [x index=0,y index=8,]{\plottedtata};
    \end{axis}
\end{tikzpicture}
\end{figure}
\end{document}

result

New contributor
wxwuse is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
1
  • Welcome to TeX.SE!
    – Mensch
    May 25 at 17:15

1 Answer 1

1

I removed the use of varwidth, I removed the use of the figure environment, and I used a stack to separate the top 4 figures from the bottom.

\documentclass{standalone}%border=3mm, 
\usepackage{tikz,stackengine}
\usepackage{subfigure}
\usepackage{pgfplotstable}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{units}

\begin{document}

\pgfplotsset{width=5cm,height=5cm,grid style={dashed}}
\begin{filecontents}{plottedtata.txt}
x    y1        y2        y3        y4        y5        y6        y7        y8
x    m         m         m         m         m         m         m         m
1    0.6951    0.6678    0.6753    0.9160    0.4609    0.4714    0.4735    0.1917
2    0.0680    0.8444    0.0067    0.0012    0.7702    0.0358    0.1527    0.7384
3    0.2548    0.3445    0.6022    0.4624    0.3225    0.1759    0.3411    0.2428
4    0.2240    0.7805    0.3868    0.4243    0.7847    0.7218    0.6074    0.9174
\end{filecontents}

\pgfplotstableread[
  skip first n=6, % 2+4 for filecontents header
]{plottedtata.txt}{\plottedtata}
%\begin{figure}
\stackunder{%
\begin{tikzpicture}
    \begin{axis}
        [color=black,grid=major,use units, y unit=m,xlabel=(1), ylabel=y,
        every major tick/.append style={thick, major tick length=3pt, black},
        every minor tick/.append style={thick, minor tick length=1.7pt, black},
        xmin=1, xmax=4, xtick={1,2,3,4}, tick align=outside, minor x tick num=1,
        ymin=0, ymax=1, minor y tick num=1]
        \addplot[draw=red,line width=1,mark=*,mark options={fill=red}] table [x index=0,y index=1,]{\plottedtata};
    \end{axis}
\end{tikzpicture}
\begin{tikzpicture}
    \begin{axis}
        [color=black,grid=major,use units, y unit=m,xlabel=(2), ylabel=y,
        every major tick/.append style={thick, major tick length=3pt, black},
        every minor tick/.append style={thick, minor tick length=1.7pt, black},
        xmin=1, xmax=4, xtick={1,2,3,4}, tick align=outside, minor x tick num=1,
        ymin=0, ymax=1, minor y tick num=1]
        \addplot[draw=red,line width=1,mark=*,mark options={fill=red}] table [x index=0,y index=2,]{\plottedtata};
    \end{axis}
\end{tikzpicture}
\begin{tikzpicture}
    \begin{axis}
        [color=black,grid=major,use units, y unit=m,xlabel=(3), ylabel=y,
        every major tick/.append style={thick, major tick length=3pt, black},
        every minor tick/.append style={thick, minor tick length=1.7pt, black},
        xmin=1, xmax=4, xtick={1,2,3,4}, tick align=outside, minor x tick num=1,
        ymin=0, ymax=1, minor y tick num=1]
        \addplot[draw=red,line width=1,mark=*,mark options={fill=red}] table [x index=0,y index=3,]{\plottedtata};
    \end{axis}
\end{tikzpicture}
\begin{tikzpicture}
    \begin{axis}
        [color=black,grid=major,use units, y unit=m,xlabel=(4), ylabel=y,
        every major tick/.append style={thick, major tick length=3pt, black},
        every minor tick/.append style={thick, minor tick length=1.7pt, black},
        xmin=1, xmax=4, xtick={1,2,3,4}, tick align=outside, minor x tick num=1,
        ymin=0, ymax=1, minor y tick num=1]
        \addplot[draw=red,line width=1,mark=*,mark options={fill=red}] table [x index=0,y index=4,]{\plottedtata};
    \end{axis}
\end{tikzpicture}
}{
\begin{tikzpicture}
    \begin{axis}
        [color=black,grid=major,use units, y unit=m,xlabel=(5), ylabel=y,
        every major tick/.append style={thick, major tick length=3pt, black},
        every minor tick/.append style={thick, minor tick length=1.7pt, black},
        xmin=1, xmax=4, xtick={1,2,3,4}, tick align=outside, minor x tick num=1,
        ymin=0, ymax=1, minor y tick num=1]
        \addplot[draw=red,line width=1,mark=*,mark options={fill=red}] table [x index=0,y index=5,]{\plottedtata};
    \end{axis}
\end{tikzpicture}
\begin{tikzpicture}
    \begin{axis}
        [color=black,grid=major,use units, y unit=m,xlabel=(6), ylabel=y,
        every major tick/.append style={thick, major tick length=3pt, black},
        every minor tick/.append style={thick, minor tick length=1.7pt, black},
        xmin=1, xmax=4, xtick={1,2,3,4}, tick align=outside, minor x tick num=1,
        ymin=0, ymax=1, minor y tick num=1]
        \addplot[draw=red,line width=1,mark=*,mark options={fill=red}] table [x index=0,y index=6,]{\plottedtata};
    \end{axis}
\end{tikzpicture}
\begin{tikzpicture}
    \begin{axis}
        [color=black,grid=major,use units, y unit=m,xlabel=(7), ylabel=y,
        every major tick/.append style={thick, major tick length=3pt, black},
        every minor tick/.append style={thick, minor tick length=1.7pt, black},
        xmin=1, xmax=4, xtick={1,2,3,4}, tick align=outside, minor x tick num=1,
        ymin=0, ymax=1, minor y tick num=1]
        \addplot[draw=red,line width=1,mark=*,mark options={fill=red}] table [x index=0,y index=7,]{\plottedtata};
    \end{axis}
\end{tikzpicture}
\begin{tikzpicture}
    \begin{axis}
        [color=black,grid=major,use units, y unit=m,xlabel=(8), ylabel=y,
        every major tick/.append style={thick, major tick length=3pt, black},
        every minor tick/.append style={thick, minor tick length=1.7pt, black},
        xmin=1, xmax=4, xtick={1,2,3,4}, tick align=outside, minor x tick num=1,
        ymin=0, ymax=1, minor y tick num=1]
        \addplot[draw=red,line width=1,mark=*,mark options={fill=red}] table [x index=0,y index=8,]{\plottedtata};
    \end{axis}
\end{tikzpicture}
}
%\end{figure}
\end{document}

enter image description here

ADDENDUM

Per OP's follow-up comment, if one needs 3 or more rows of figures, use the syntax \Shorstack{<first row>\\<second row>\\<third row>...}, where the row separator is set via \setstackEOL{\\} (default <space>). The interrow gap can be pre-set via \setstackgap{S}{6pt}. The default alignment is centered, which can be reset with \renewcommand\stackalignment{l or c or r}.

\documentclass{standalone}%border=3mm, 
\usepackage{tikz,stackengine}
\usepackage{subfigure}
\usepackage{pgfplotstable}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{units}

\begin{document}

\pgfplotsset{width=5cm,height=5cm,grid style={dashed}}
\begin{filecontents}{plottedtata.txt}
x    y1        y2        y3        y4        y5        y6        y7        y8
x    m         m         m         m         m         m         m         m
1    0.6951    0.6678    0.6753    0.9160    0.4609    0.4714    0.4735    0.1917
2    0.0680    0.8444    0.0067    0.0012    0.7702    0.0358    0.1527    0.7384
3    0.2548    0.3445    0.6022    0.4624    0.3225    0.1759    0.3411    0.2428
4    0.2240    0.7805    0.3868    0.4243    0.7847    0.7218    0.6074    0.9174
\end{filecontents}

\pgfplotstableread[
  skip first n=6, % 2+4 for filecontents header
]{plottedtata.txt}{\plottedtata}
%\begin{figure}
\setstackgap{S}{6pt}
\setstackEOL{\\}
\Shortstack{%
\begin{tikzpicture}
    \begin{axis}
        [color=black,grid=major,use units, y unit=m,xlabel=(1), ylabel=y,
        every major tick/.append style={thick, major tick length=3pt, black},
        every minor tick/.append style={thick, minor tick length=1.7pt, black},
        xmin=1, xmax=4, xtick={1,2,3,4}, tick align=outside, minor x tick num=1,
        ymin=0, ymax=1, minor y tick num=1]
        \addplot[draw=red,line width=1,mark=*,mark options={fill=red}] table [x index=0,y index=1,]{\plottedtata};
    \end{axis}
\end{tikzpicture}
\begin{tikzpicture}
    \begin{axis}
        [color=black,grid=major,use units, y unit=m,xlabel=(2), ylabel=y,
        every major tick/.append style={thick, major tick length=3pt, black},
        every minor tick/.append style={thick, minor tick length=1.7pt, black},
        xmin=1, xmax=4, xtick={1,2,3,4}, tick align=outside, minor x tick num=1,
        ymin=0, ymax=1, minor y tick num=1]
        \addplot[draw=red,line width=1,mark=*,mark options={fill=red}] table [x index=0,y index=2,]{\plottedtata};
    \end{axis}
\end{tikzpicture}
\begin{tikzpicture}
    \begin{axis}
        [color=black,grid=major,use units, y unit=m,xlabel=(3), ylabel=y,
        every major tick/.append style={thick, major tick length=3pt, black},
        every minor tick/.append style={thick, minor tick length=1.7pt, black},
        xmin=1, xmax=4, xtick={1,2,3,4}, tick align=outside, minor x tick num=1,
        ymin=0, ymax=1, minor y tick num=1]
        \addplot[draw=red,line width=1,mark=*,mark options={fill=red}] table [x index=0,y index=3,]{\plottedtata};
    \end{axis}
\end{tikzpicture}
\\
\begin{tikzpicture}
    \begin{axis}
        [color=black,grid=major,use units, y unit=m,xlabel=(4), ylabel=y,
        every major tick/.append style={thick, major tick length=3pt, black},
        every minor tick/.append style={thick, minor tick length=1.7pt, black},
        xmin=1, xmax=4, xtick={1,2,3,4}, tick align=outside, minor x tick num=1,
        ymin=0, ymax=1, minor y tick num=1]
        \addplot[draw=red,line width=1,mark=*,mark options={fill=red}] table [x index=0,y index=4,]{\plottedtata};
    \end{axis}
\end{tikzpicture}
\begin{tikzpicture}
    \begin{axis}
        [color=black,grid=major,use units, y unit=m,xlabel=(5), ylabel=y,
        every major tick/.append style={thick, major tick length=3pt, black},
        every minor tick/.append style={thick, minor tick length=1.7pt, black},
        xmin=1, xmax=4, xtick={1,2,3,4}, tick align=outside, minor x tick num=1,
        ymin=0, ymax=1, minor y tick num=1]
        \addplot[draw=red,line width=1,mark=*,mark options={fill=red}] table [x index=0,y index=5,]{\plottedtata};
    \end{axis}
\end{tikzpicture}
\begin{tikzpicture}
    \begin{axis}
        [color=black,grid=major,use units, y unit=m,xlabel=(6), ylabel=y,
        every major tick/.append style={thick, major tick length=3pt, black},
        every minor tick/.append style={thick, minor tick length=1.7pt, black},
        xmin=1, xmax=4, xtick={1,2,3,4}, tick align=outside, minor x tick num=1,
        ymin=0, ymax=1, minor y tick num=1]
        \addplot[draw=red,line width=1,mark=*,mark options={fill=red}] table [x index=0,y index=6,]{\plottedtata};
    \end{axis}
\end{tikzpicture}
\\
\begin{tikzpicture}
    \begin{axis}
        [color=black,grid=major,use units, y unit=m,xlabel=(7), ylabel=y,
        every major tick/.append style={thick, major tick length=3pt, black},
        every minor tick/.append style={thick, minor tick length=1.7pt, black},
        xmin=1, xmax=4, xtick={1,2,3,4}, tick align=outside, minor x tick num=1,
        ymin=0, ymax=1, minor y tick num=1]
        \addplot[draw=red,line width=1,mark=*,mark options={fill=red}] table [x index=0,y index=7,]{\plottedtata};
    \end{axis}
\end{tikzpicture}
\begin{tikzpicture}
    \begin{axis}
        [color=black,grid=major,use units, y unit=m,xlabel=(8), ylabel=y,
        every major tick/.append style={thick, major tick length=3pt, black},
        every minor tick/.append style={thick, minor tick length=1.7pt, black},
        xmin=1, xmax=4, xtick={1,2,3,4}, tick align=outside, minor x tick num=1,
        ymin=0, ymax=1, minor y tick num=1]
        \addplot[draw=red,line width=1,mark=*,mark options={fill=red}] table [x index=0,y index=8,]{\plottedtata};
    \end{axis}
\end{tikzpicture}
}
%\end{figure}
\end{document}

enter image description here

6
  • Dear Steven, thank you very much for your answer, it really solves this question. But I find that, if we have more figures, for example, if we want to have 3 rows and 4 columns, that is 12 figures in total. Then the "9th-to-12th" figures will not be below the "5th-to-8th" figures as we expect, but on the right of the "1st-to-4th" ones. In the more figures example, how could we obtain the effect?
    – wxwuse
    May 25 at 21:17
  • @wxwuse Per the stackengine documentation, use \setstackEOL{\\} to make the double-backslash (or any other convenient syntax) a row separator, and then use \Shortstack{<first row content>\\<second row content>\\<third row content>...}. The dead space between stacked rows is controlled by (and can be pre-set with) \setstackgap{S}{<gap value>} (default 3pt). May 25 at 23:04
  • @wxwuse I have provided an ADDENDUM to explain this concept. May 25 at 23:11
  • Dear Steven, thank you again for your answer, it really solves this question.
    – wxwuse
    May 26 at 12:08
  • @wxwuse You are welcome. In order to best indicate to the community that a question of yours has been answered to your satisfaction, you may click the check mark to the left of the one answer (if there are several answers) that best answers your question. This is called "accepting" an answer. May 26 at 12:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .