如何使用 for 循环在 latex 中创建多个图

如何使用 for 循环在 latex 中创建多个图

我有一堆如下的数组:

a[l=3]=
(1, 8.099681e-01) (2, 8.099681e-01) (3, 7.399681e-01) (4, 0.000000e+00)
(1, 9.600000e-01) (2, 9.600000e-01) (3, 8.800000e-01) (4, 0.000000e+00)

a[l=4]=
(1, 8.099681e-01) (2, 8.099681e-01) (3, 7.699681e-01) (4, 0.000000e+00)
(1, 9.600000e-01) (2, 9.600000e-01) (3, 9.200000e-01) (4, 0.000000e+00)

我想要为每个数组自动创建一些图形(因为我有很多)。我想要创建的图如下:

    \begin{figure}
        \subfigure[$l=3$]{ % array l=3
        \begin{tikzpicture}
        \begin{axis}[]
        \addplot[]
            coordinates { % Precision
(1, 8.099681e-01) (2, 8.099681e-01) (3, 7.399681e-01) (4, 0.000000e+00)
            };
        \addplot[]
            coordinates { % Recall
            (1, 9.600000e-01) (2, 9.600000e-01) (3, 8.800000e-01) (4, 0.000000e+00)
            };
            \legend{Precision, Recall}
        \end{axis}
        \end{tikzpicture}
        }
        \subfigure[$l=4$]{ % array l=4
        \begin{tikzpicture}
        \begin{axis}[]
        \addplot[]
            coordinates { % Precision
            (1, 8.099681e-01) (2, 8.099681e-01) (3, 7.699681e-01) (4, 0.000000e+00)
            };
        \addplot[]
            coordinates { % Recall
            (1, 9.600000e-01) (2, 9.600000e-01) (3, 9.200000e-01) (4, 0.000000e+00)
            };
            \legend{Precision, Recall}
        \end{axis}
        \end{tikzpicture}
        }
    \end{figure}

这些数组的格式可以是原始的或任何类型,但我希望我的 latex 文件简单一些。我想知道是否有办法使用 for 循环来处理这个问题并摆脱重复的 tikz 内容?

\foreach \l in {3, ..., 15}
{%do something about array l
    \l
}%

答案1

仅供参考,我使用forlatex 中的循环来完成所有这些绘图。我首先引入了一个命令,然后编写了一个 for 循环来读取数据并创建绘图。

\documentclass[12pt]{article} \linespread{1.65}
\usepackage{subfigure}
\usepackage{pgfplots}
\usepackage{arrayjob}


\newcommand{\plotprh}[3] {
\subfigure[$\ell=#1$]{%
    \begin{tikzpicture}
    \begin{axis}[
        height=4.2cm, 
        width=5.5cm,
        xlabel={$k'$},
        ylabel={Percent (\%)},
        legend pos=north west,
        legend cell align=left,
        ymin=0,
        ylabel style={at={(0.09,0.5)},font=\small},
        xlabel style={at={(0.5,0.04)},font=\small},
        legend style={at={(0.42,0.06)},anchor=south west, nodes={scale=0.8, transform shape},font=\small,}
    ]
    \addplot[
        color=black,
        mark=square,
        mark size=3,
        ]
        coordinates { % Precision
        #2
        };
    \addplot[
        color=blue,
        mark=diamond,
        mark size=3,
        ]
        coordinates { % Recall
        #3
        };
        \legend{Precision, Recall}
    \end{axis}
    \end{tikzpicture}
    }
  }


\def\plotData{%
    {3}/{(1,8.627451e+01)(2,6.274510e+01)(3,0.000000e+00)(4,0.000000e+00)}/{(1,1.000000e+02)(2,1.000000e+02)(3,0.000000e+00)(4,0.000000e+00)},
    {4}/{(1,9.215686e+01)(2,7.843137e+01)(3,0.000000e+00)(4,0.000000e+00)}/{(1,1.000000e+02)(2,1.000000e+02)(3,0.000000e+00)(4,0.000000e+00)},
    {5}/{(1,9.607843e+01)(2,8.627451e+01)(3,5.686275e+01)(4,0.000000e+00)}/{(1,1.000000e+02)(2,1.000000e+02)(3,1.000000e+02)(4,0.000000e+00)},
    {6}/{(1,9.803922e+01)(2,9.019608e+01)(3,7.058824e+01)(4,0.000000e+00)}/{(1,1.000000e+02)(2,1.000000e+02)(3,1.000000e+02)(4,0.000000e+00)},
    {7}/{(1,1.000000e+02)(2,9.215686e+01)(3,7.843137e+01)(4,5.294118e+01)}/{(1,9.622642e+01)(2,1.000000e+02)(3,1.000000e+02)(4,1.000000e+02)},
    {8}/{(1,1.000000e+02)(2,9.607843e+01)(3,8.627451e+01)(4,5.882353e+01)}/{(1,9.107143e+01)(2,1.000000e+02)(3,1.000000e+02)(4,1.000000e+02)},
    {9}/{(1,1.000000e+02)(2,9.607843e+01)(3,8.823529e+01)(4,7.058824e+01)}/{(1,8.095238e+01)(2,1.000000e+02)(3,1.000000e+02)(4,1.000000e+02)},
    {10}/{(1,1.000000e+02)(2,9.803922e+01)(3,9.019608e+01)(4,7.843137e+01)}/{(1,7.391304e+01)(2,1.000000e+02)(3,1.000000e+02)(4,1.000000e+02)},
    {11}/{(1,1.000000e+02)(2,9.803922e+01)(3,9.215686e+01)(4,8.627451e+01)}/{(1,6.144578e+01)(2,1.000000e+02)(3,1.000000e+02)(4,1.000000e+02)},
    {12}/{(1,1.000000e+02)(2,1.000000e+02)(3,9.607843e+01)(4,8.823529e+01)}/{(1,5.543478e+01)(2,9.622642e+01)(3,1.000000e+02)(4,1.000000e+02)},
    {13}/{(1,1.000000e+02)(2,1.000000e+02)(3,9.607843e+01)(4,8.823529e+01)}/{(1,4.636364e+01)(2,9.622642e+01)(3,1.000000e+02)(4,1.000000e+02)},
    {14}/{(1,1.000000e+02)(2,1.000000e+02)(3,9.607843e+01)(4,9.019608e+01)}/{(1,4.146341e+01)(2,9.272727e+01)(3,1.000000e+02)(4,1.000000e+02)}}



\begin{document}


\begin{figure}[p]
    \centering
    \foreach \el/\recall/\precision in \plotData
    {%
        \plotprh{\el}{\precision}{\recall}%
    }%
    \caption{Results}
    \label{results}
\end{figure}

\end{document}

相关内容