如何在一个 PDF 文件中水平和垂直对齐多个 tikzpicture

如何在一个 PDF 文件中水平和垂直对齐多个 tikzpicture

我希望对齐 tikzpictures 绘制的多个图形。

如下面的代码所示,我画了八个图形,它们自动显示在 4 行 2 列中,但我希望它们显示在 2 行 4 列中。是否有相关的方法可以按我们想要的方式对齐它们,例如本例中的 2 行 4 列?

此外,我想将它们放在一个 PDF 文件中,并且没有边框或边框很窄,因此我对 \documentclass 使用 {standalone}。

非常感谢

\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}

结果

答案1

我删除了的使用varwidth,我删除了环境的使用figure,并且我使用堆栈将顶部 4 个数字与底部分开。

\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}

在此处输入图片描述

附录

根据 OP 的后续评论,如果需要 3 行或更多行图形,请使用语法\Shorstack{<first row>\\<second row>\\<third row>...},其中行分隔符通过\setstackEOL{\\}(默认 <space>)设置。行间间隙可以通过预设\setstackgap{S}{6pt}。默认对齐方式为居中,可以使用\renewcommand\stackalignment{lc或重置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}

在此处输入图片描述

相关内容